tableView:didEndEditingRowAtIndexPath:委托方法调用两次

时间:2014-11-17 13:47:45

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

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

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

willBegin...被调用一次,而didEnd...被调用两次。这有什么理由吗?

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

任何提示?

2 个答案:

答案 0 :(得分:3)

我的解决方案在我的博客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)
    }
}

答案 1 :(得分: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!
    }
}

这更像是一种解决方法,但却完全出现这种情况非常令人沮丧。