UILongPressGestureRecognizer *longGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(editPathAction:)];
longGestureRecognizer.minimumPressDuration = 0.7;
longGestureRecognizer.numberOfTouchesRequired = 1;
longGestureRecognizer.view.tag = indexPath.row;
cell.contentView.tag = indexPath.row;
[cell.contentView addGestureRecognizer:longGestureRecognizer];
在 tableViewCell 上添加了一个长按手势,在手势方法 editPathAction: 中需要确认是哪一个cell触发了方法,cell.contentView.tag = indexPath.row 给cell一个 tag 值,在 editPathAction: 方法中获取cell
- (void)editPathAction:(UILongPressGestureRecognizer *)longGestureRecognizer{
//长按会两次触发手势方法,可通过 longGestureRecognizer.state == UIGestureRecognizerStateBegan 判断来解决该问题
if (longGestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSInteger index = longGestureRecognizer.view.tag;
NSDictionary *dic = _dataSource[index];
}
}
NSInteger index = longGestureRecognizer.view.tag ,可以获取到触发方法的 cell 的 tag 值,即可确定是哪一个 cell 触发了方法,也就能获取到 cell 的数据。
***其中,longGestureRecognizer.view 就是添加手势的那个 view,此处即是 cell.contentView。