75

我有一个UIViewController实现 TableViews 委托和数据源协议的。现在我想向单元格添加“滑动删除”手势。

我该怎么办。

我给出了一个空白的commitEditingStyle方法实现,并将 Editing 属性设置为 YES。

仍然没有刷卡功能。

现在我需要单独添加UISwipeGesture到每个单元格吗?

还是我错过了什么?

4

13 回答 13

61

正如Dan上面评论的那样,您需要实现以下 table view 委托方法:

  1. tableView:canEditRowAtIndexPath:
  2. tableView:commitEditingStyle:forRowAtIndexPath:

注意:我已经在 iOS 6 和 iOS 7 中尝试过。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform the real delete action here. Note: you may need to check editing style
    //   if you do not perform delete only.
    NSLog(@"Deleted row.");
}
于 2013-10-07T09:20:30.340 回答
55

editing:YES如果您需要在单元格滑动时显示删除按钮,则无需设置。tableView:canEditRowAtIndexPath:对于需要编辑/删除的行,您必须从那里实施并返回 YES。当您的 tableView 的 dataSource 是 UITableViewContoller 的子类时,这不是必需的 - 如果未覆盖此方法,则默认返回 YES。在所有其他情况下,您必须实施它。

编辑:我们一起发现了问题 -如果表未处于编辑模式,则tableView:editingStyleForRowAtIndexPath:返回。UITableViewCellEditingStyleNone

于 2012-01-24T07:07:47.067 回答
25
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
于 2013-12-15T00:01:28.120 回答
12

请快速尝试此代码,

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
   // let the controller to know that able to edit tableView's row 
   return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  {
   // if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?  {
   // add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
   let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in

   // Your delete code here.....
   .........
   .........
   })

   // You can set its properties like normal button
   deleteAction.backgroundColor = UIColor.redColor()

   return [deleteAction]
}
于 2015-07-08T16:16:20.430 回答
5

尝试将以下内容添加到您的课程中:

// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return(YES);
}
于 2012-01-24T07:08:31.733 回答
3

Kyr Dunenkoff聊天的结论是

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

}

如果您需要在滑动时显示删除按钮,则不应定义。

于 2013-06-14T18:10:19.083 回答
1

如果您使用 anNSFetchedResultsControllerDelegate来填充表格视图,这对我有用:

  • 确保tableView:canEditRowAtIndexPath始终返回 true
  • 在您的tableView:commitEditingStyle:forRowAtIndexPath实现中,不要直接从表视图中删除该行。相反,使用您的托管对象上下文删除它,例如:

    if editingStyle == UITableViewCellEditingStyle.Delete {
        let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word
        self.managedObjectContext.deleteObject(word)
        self.saveManagedObjectContext()
    }
    
    func saveManagedObjectContext() {
        do {
            try self.managedObjectContext.save()
        } catch {
            let saveError = error as NSError
            print("\(saveError), \(saveError.userInfo)")
        }
    }
    
于 2016-02-17T22:05:13.033 回答
0

这对我来说也是一个问题......我只能每 10 次左右尝试刷卡删除才能工作一次。事实证明,gesture电视上的 父视图控制器中的另一个手势被阻止了。电视嵌套在MMDrawerController(可滑动的抽屉布局)中。

只需将抽屉控制器中的手势识别器配置为不响应侧面抽屉中的关闭手势,就可以在我的电视中进行滑动删除操作。

您也可以尝试使用以下方法执行以下操作gesture delegate

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
于 2014-05-06T22:13:49.573 回答
0

这是快捷版

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
于 2015-05-06T09:22:20.037 回答
0

editing根据我的经验,似乎您必须UITableView准备好NO才能刷卡上班。

self.tableView.editing = NO;

于 2016-06-09T16:17:37.640 回答
0

在 iOS 8.0 之后,您可以自定义您的操作

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
于 2017-01-04T07:03:29.450 回答
-1
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}

- (void)tableView:(UITableView *)tableView 
                    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
                    forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {

        [posts removeObjectAtIndex:row];
    }
}
于 2014-10-20T04:59:47.060 回答
-2

您可以通过在 XCode 5 中创建 UITableViewController 类(临时)来查看所有必要的方法,然后复制您想要使用的方法。您需要的那些方法将用您想要的预填充行注释掉。

于 2014-04-23T14:43:36.763 回答