【问题标题】:After a swipe left on a UITableViewCell in a UITableView, how do I programmatically close the swipe?在 UITableView 中的 UITableViewCell 上向左滑动后,如何以编程方式关闭滑动?
【发布时间】:2014-10-29 06:26:28
【问题描述】:

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

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

【问题讨论】:

  • 尝试重新加载单元格——类似于:tableViews 的reloadCellAtIndexPath

标签: ios xcode ios8


【解决方案1】:

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

如果你想关闭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];
}

使用performSelector:withObject:afterDelay:的原因是Table View Programming Guide for iOS中的注释

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

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

【讨论】:

  • 折腾了好久,发现关键是调用tableView.setEditing(),在table view上设置editing为false。我之前在不起作用的单元格上将编辑设置为 false。
  • 是的,这就是在 Objective C 中使用 [self.tableView setEditing:NO animated:YES]; 或在 Swift 中使用 self.tableView.setEditing(false, animated: true) 的答案背后的想法。抱歉,最初我没有发现您使用的是 Swift
猜你喜欢
  • 2016-07-06
  • 2011-08-24
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2012-08-10
  • 2016-04-28
相关资源
最近更新 更多