[ios]tableViewCell的 长按操作

思路1:

对cell增加长按手势。

失败,cell长按手势无法触发。

思路2:

1.对tableView增加 长按手势

2.通过手势获取point

3.通过point获取row

增加手势

 longPress = [[UILongPressGestureRecognizer alloc]

                                               initWithTarget:self

                                               action:@selector(myHandleTableviewCellLongPressed:)];

 

    longPress.minimumPressDuration = 1.0;

    [self.tableView addGestureRecognizer:longPress];

……

CGPoint ponit=[gestureRecognizer locationInView:self.tableView];

//获取手势在传入的view的位置  (这里是单点)

//多点- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view; 

 

NSIndexPath* path=[self.tableView indexPathForRowAtPoint:ponit];

//根据点击的point在table中找到相应的indexpath。

 

 

完整

-(void)tableviewCellLongPressed:(UILongPressGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
        CGPoint ponit=[gestureRecognizer locationInView:self.tableView];
        NSLog(@" CGPoint ponit=%f %f",ponit.x,ponit.y);
        NSIndexPath* path=[self.tableView indexPathForRowAtPoint:ponit];
        NSLog(@"row:%d",path.row);
        currRow=path.row;
    }else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        //未用
    }
    else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        //未用
    }

    
}

 

猜你喜欢

转载自poolo.iteye.com/blog/1843258