在UITableView中的UITableViewCell上滑动后,如何以编程方式关闭滑动?

Sam*_*Sam 4 xcode ios ios8

我在一行上启用向左滑动以显示操作.点击动作后,我希望刷开的表格单元格关闭.我该怎么做呢?

我试过cell.setEditing(false, animated:true)但是设置编辑并没有关闭滑动.

Geo*_*rgi 6

注意:我没有足够的声誉来评论您的问题,因此我必须对您的实施做出一些假设.

如果您尝试关闭单元格,tableView:commitEditingStyle:forRowAtIndexPath:可以尝试这样做:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // remove delete button only after short delay
        [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
    }
}

- (void)hideDeleteButton:(id)obj
{
    [self.tableView setEditing:NO animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

使用的原因performSelector:withObject:afterDelay:iOS视图编程指南中的注释

注意:数据源不应该调用setEditing:animated:在tableView的实现中:commitEditingStyle:forRowAtIndexPath:.如果由于某种原因它必须,它应该在延迟后使用performSelector:withObject:afterDelay:方法调用它.

所有这些都受到以下答案的启发:https: //stackoverflow.com/a/22063692/2433921

  • 是的,这就是使用`[self.tableView setEditing:NO animated:YES];在Objective C中的`或Swift中的`self.tableView.setEditing(false,animated:true)`的答案背后的想法.对不起,我最初没有注意到你使用的是Swift (2认同)