首页 > 技术文章 > IOS 关于tableview中cell的长按手势

dujiahong 2019-04-09 14:57 原文

说明:虽然是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];
        
    }
}

 

推荐阅读