說明:雖然是tableview中cell的長按手勢 但是手勢是添加在tableview上的
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressAction:)]; [self.tableView addGestureRecognizer:longpress]; - (void)pressAction:(UILongPressGestureRecognizer *)longPressGesture { if (longPressGesture.state == UIGestureRecognizerStateBegan) {//手勢開始 CGPoint point = [longPressGesture locationInView:self.tableView]; NSIndexPath *currentIndexPath = [self.tableView indexPathForRowAtPoint:point]; // 可以獲取我們在哪個cell上長按 NSLog(@"%ld",currentIndexPath.section); } if (longPressGesture.state == UIGestureRecognizerStateEnded)//手勢結束 { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否刪除" message:nil delegate:self cancelButtonTitle:@"確認" otherButtonTitles:@"取消", nil]; [alert show]; } }