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觸發(fā)了方法,cell.contentView.tag = indexPath.row 給cell一個 tag 值,在 editPathAction: 方法中獲取cell
- (void)editPathAction:(UILongPressGestureRecognizer *)longGestureRecognizer{
//長按會兩次觸發(fā)手勢方法,可通過 longGestureRecognizer.state == UIGestureRecognizerStateBegan 判斷來解決該問題
if (longGestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSInteger index = longGestureRecognizer.view.tag;
NSDictionary *dic = _dataSource[index];
}
}
NSInteger index = longGestureRecognizer.view.tag ,可以獲取到觸發(fā)方法的 cell 的 tag 值,即可確定是哪一個 cell 觸發(fā)了方法,也就能獲取到 cell 的數(shù)據(jù)。
***其中,longGestureRecognizer.view 就是添加手勢的那個 view,此處即是 cell.contentView。