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