如何启用滑动以删除TableView中的单元格?

Amo*_*kar 75 uitableview ios uiswipegesturerecognizer

我有一个UIViewController实现TableViews委托和数据源协议.现在我想向细胞添加"轻扫删除"手势.

我应该怎么做呢.

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

仍然没有滑动功能.

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

或者我错过了什么?

Dj *_*j S 61

正如Dan上面所述,您需要实现以下表视图委托方法:

  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.");
}
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚在iOS 6上在iPhone 6上测试了它,它可以工作.我只需要实现` - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath`方法,默认情况下编辑应为YES. (3认同)

Kyr*_*off 53

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

编辑:我们一起发现了问题- tableView:editingStyleForRowAtIndexPath:返回UITableViewCellEditingStyleNone如果表中编辑模式没有.

  • 我想补充一点,`tableView:canEditRowAtIndexPath:`可能还不够.您还必须实现`tableView:commitEditingStyle:forRowAtIndexPath`. (5认同)
  • 如果不删除它,则将表格置于编辑状态,其中滑动不起作用.它可以是左边的圆形红色减号按钮,也可以是滑动,而不是一起. (3认同)

Can*_*soy 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
    }   
}
Run Code Online (Sandbox Code Playgroud)

  • 像我这样的新手值得注意/评论:仍然需要删除行,以及需要减少数据源(例如数组). (5认同)

Mas*_*iYa 13

请在swift中尝试此代码,

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]
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*dek 5

尝试将以下内容添加到您的班级:

// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return(YES);
}
Run Code Online (Sandbox Code Playgroud)

  • 不适合我:(是因为我的类是UIViewController的子类,我需要做一些额外的UITabelViewController吗? (5认同)