UITableView禁用滑动删除,但在编辑模式下仍然有删除?

wil*_*lli 91 cocoa-touch uitableview uikit ios

我想要类似于闹钟应用程序的东西,你不能滑动删除行,但你仍然可以删除编辑模式中的行.

当注释掉tableView:commitEditingStyle:forRowAtIndexPath:时,我禁止滑动删除并在编辑模式下仍然有删除按钮,但是当我按下删除按钮时会发生什么.叫什么?

wil*_*lli 283

好吧,结果很简单.这就是我为解决这个问题所做的:

Objective-C的

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Detemine if it's in editing mode
    if (self.tableView.editing)
    {
        return UITableViewCellEditingStyleDelete;
    }

    return UITableViewCellEditingStyleNone;
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if tableView.editing {
         return .Delete
    }

    return .None
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    if tableView.isEditing {
        return .delete
    }

    return .none
}
Run Code Online (Sandbox Code Playgroud)

您仍然需要实现tableView:commitEditingStyle:forRowAtIndexPath:提交删除.

  • 忘了在commitEditingStyle中提到你需要if(editingStyle == UITableViewCellEditingStyleDelete): (3认同)

Mar*_*ind 9

为了清楚起见,除非tableView:commitEditingStyle:forRowAtIndexPath:实施,否则不会启用滑动到删除.

当我在开发中时,我没有实现它,因此没有启用滑动到删除.当然,在完成的应用程序中,它将始终实现,因为否则将不会进行编辑.


小智 5

您需要实现 CanEditRowAt 函数。

您可以在 EditingStyleForRowAt 函数中返回 .delete,这样您仍然可以在编辑模式下删除。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if tableView.isEditing {
        return true
    }
    return false
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}
Run Code Online (Sandbox Code Playgroud)