使用 editActionsForRowAtIndexPath: 时删除显示的 UITableViewCell 滑动的正确方法是什么?

What is the correct way to remove the revealed swipe of a UITableViewCell when using editActionsForRowAtIndexPath:?

我有一个 tableView,我在其中启用滑动单元格以显示选项 - 在我的例子中,使用以下方法 'share' 和 'delete' 选项:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

当用户在单元格上滑动以显示选项然后点击 delete 时,我会发出 UIAlertController 以进行确认。如果他们说删除,一切都很好,因为我删除了我的核心数据数据库中的对象,我的 NSFetchedResultController 负责更新 tableView

如果用户拒绝执行删除操作,则单元格将保留幻灯片效果并显示选项。我希望它消失。我知道 [tableView reloadData] 或者,更有效地,只需重新加载一个单元格就可以解决问题,但是我错过的单元格上是否有方法调用或 属性 可以完成这项工作?

那么,您的问题是关于如何重新加载 UITableView 上的一个特定单元格?不难。

以下代码可以解决问题:-

NSIndexPath * indexpath = [NSIndexPath indexPathForRow:self.deletedTableRow
                                             inSection: self.deletedTableSection];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexpath] 
                      withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

如果您在此处结束对 table 视图的编辑,它将使单元格恢复到正常状态。

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    [table setEditing:NO animated:YES];
}

如果 table 处于编辑状态,在每个单元格旁边显示删除按钮,这将导致所有这些删除按钮消失,这可能不是您想要的行为。在这种情况下,您可以立即将 table 视图设置回编辑模式。动画看起来不错:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    [table setEditing:NO animated:YES];
    if (yourWasPreviouslyEditingBoolean) {
        [table setEditing:YES animated:YES];
    }
}

请注意,您需要自己跟踪此状态。检查 table 视图的 isEditing 值在这里总是 return true。

我一直没能找到办法直接告诉单元格停止编辑。

解决了在导航栏上放置自定义后退按钮的相同问题。我有 UITableViewController 和 UINavigationController。 "custom back button" 操作(如此实现)的行为类似于 willDisappear。

注意滑动 "go back" 手势。