【问题标题】:tableView:didEndEditingRowAtIndexPath: delegate method called twicetableView:didEndEditingRowAtIndexPath: 委托方法调用了两次
【发布时间】:2014-11-17 13:47:45
【问题描述】:

我想知道用户何时对UITableView 的单元格应用滑动操作。根据文档,我应该使用的 UITableViewDelegate 方法如下:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

willBegin... 被调用一次,didEnd... 被调用两次。这有什么原因吗?

我的目标是知道用户何时在单元格上执行了滑动手势,然后是取消手势(他不想删除任何内容)。这是为了在未执行任何操作时恢复先前选择的单元格(根据UITableView loses selection)。

有什么提示吗?

【问题讨论】:

  • 我没有看到这种行为。创建一个功能最少的简单表格视图。你还看到这种行为吗?也许您当前的代码正在做一些导致双重调用的事情。
  • @rmaddy 我用 UITableView 设置了一个最小的示例代码,并且行为仍然存在。这发生在模拟器上。我会在设备上尝试。谢谢。
  • @rmaddy 问题仅限于模拟器。我目前使用的是 8.1。够奇怪的。
  • 我收回了。在 iOS 8.1 下(至少)我看到了两个对didEndEditing 的调用。这是在真实设备上。我有一个包含一行的表格视图。我做了滑动删除。 willBeginEditing 被调用。删除按钮出现。然后我点击了行外的表格视图,didEndEditing 被调用了两次。我想知道这是否是 iOS 8 的问题。
  • @rmaddy 我正在运行 iOS 7 设备,其中源代码是使用 iOS 8.1 和 xCode 6.1 编译的。我只看到一个电话。我没有尝试使用 iOS 8 设备。我应该上传雷达。

标签: ios objective-c uitableview ios7 ios8.1


【解决方案1】:

我的博客Restore the selection of a UITableViewCell after cancelling the “Swipe to delete” operation(2014 年 12 月 22 日)中描述了我的解决方案。总而言之,使用一个布尔值来跟踪操作。

我打开了一个雷达。我会等待回复,我会根据反馈进行更新。

func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {

    self.swipeGestureStarted = true
}

func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
    if(self.swipeGestureStarted) {
        self.swipeGestureStarted = false

        self.tableView.selectRowAtIndexPath(self.selectedIndexPath, animated: true, scrollPosition: .None)
    }
}

【讨论】:

    【解决方案2】:

    我也遇到了这个问题,并且能够通过将 BOOL 声明为我的视图控制器的成员来解决它:

    @interface ViewController ()
    
    @property (nonatomic, assign) BOOL isEditingRow;
    
    @end
    
    @implementation ViewController
    
    ...
    

    ...然后在 UITableView 的委托方法中设置和读取 BOOL 的值:

    -(void)tableView: (UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
    {
        self.isEditingRow = YES;
    }
    
    -(void)tableView: (UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath
    {
        if (self.isEditingRow)
        {
            self.isEditingRow = NO;
    
            // now do processing that you want to do once - not twice!
        }
    }
    

    这更像是一种解决方法,但发生这种情况非常令人沮丧。

    【讨论】:

    • 这是我不久前写的关于这个主题的帖子。我还创建了一个雷达。 cocoaperspectives.tumblr.com
    • @flexaddicted 很高兴我们得出了相同的结论 :)
    猜你喜欢
    • 2013-11-22
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 2014-07-03
    • 2013-07-10
    • 2015-04-11
    • 2012-04-08
    相关资源
    最近更新 更多