0
votes

I am adding a gesture to the cell like this

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureSection:)];

[gesture view].tag = indexPath.row;
gesture.minimumPressDuration = 0.5;
gesture.allowableMovement = 600;
[cell addGestureRecognizer:gesture];

and I am trying to get the indexPath.row

  • (void)handleGestureSection:(UIGestureRecognizer *)gesture {

    if (gesture.state == UIGestureRecognizerStateEnded) { NSLog(@"Long press Ended");

    } else if (gesture.state == UIGestureRecognizerStateBegan) { NSInteger i = [gesture view].tag; } }

But is always returning 0. What am I doing wrong?

1

1 Answers

0
votes

You get 0 because view from UIGestureRecognizer is nil.

First you should add UIGestureRecognizer for every cell.

UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
longPressGesture.delegate = cell;
[longPressGesture setMinimumPressDuration:2.0];
[cell addGestureRecognizer:longPressGesture];

And then, handle gestureHandler:

UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
CGPoint p = [recognizer locationInView:self.tableView];    
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];