为UITableView添加长按事件,长按cell时获取到当前位置:
1. 为UITableView添加长按手势识别器
//添加长按事件 UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressTableViewCell:)]; gesture.minimumPressDuration = 1.0; [self.tableView addGestureRecognizer:gesture];
2. 实现长按事件方法
//tableViewCell长按事件 - (void)didLongPressTableViewCell:(UILongPressGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { //获取到点击的位置 CGPoint point = [gesture locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; if(indexPath == nil) return; //do something... } }