爲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... } }