2

I'm trying to detect touch and hold gestures inside a UITableView, while keeping track of which cell was selected. I need to be able to differentiate between normal taps on a cell and touches that last longer than X seconds (probably 1s or so). The main challenge is that I'd like to do this without subclassing UITableViewCell, since doing so slowed down my scrolling significantly. I think there's probably a way to do this using an NSTimer, but I can't seem to get it working correctly. Using touchesBegan: and touchesEnded: with a timer is out, since I don't see any way to keep track of which cell was selected, unless there's some way to do that with those methods? Any help would be greatly appreciated.

Thanks in advance.

Matt
  • 4,849
  • 3
  • 26
  • 23
  • 2
    For OS 3.2 or later, you can use UILongPressGestureRecognizer. See http://stackoverflow.com/questions/3924446/long-press-on-uitableview/3924965#3924965. –  Nov 10 '10 at 16:57

3 Answers3

18

If we are talking about cells, you may want to get the indexPath of the cell that has been pressed.

Add the gesture recognizer to the cell, right after a new instance has been allocated:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[cell addGestureRecognizer:longPress];
[longPress release];

Handle the long press event, and get the indexPath:

-(void) handleLongPress: (UIGestureRecognizer *)longPress {
    if (longPress.state==UIGestureRecognizerStateBegan) {
        CGPoint pressPoint = [longPress locationInView:table];
        NSIndexPath *indexPath = [table indexPathForRowAtPoint:pressPoint];
    }
}
Sagiftw
  • 1,658
  • 4
  • 21
  • 25
  • It may be slightly better to add recognizer to cell.contentView, otherwise it will interfere with edit mode. – Hrissan Jun 07 '13 at 21:41
7

UILongPressGestureRecognizer is made for exactly this thing. You an create one and add it to the UITableViewCell to handle long pressses.

Rengers
  • 14,911
  • 1
  • 36
  • 54
  • This is perfect, I didn't realize this even existed and didn't see it in the documentation. Thank you very much! – Matt Nov 10 '10 at 18:52
3

Short answer: Subclass and use a UILongPressGestureRecognizer.

Longer answer: I believe the reason you are having scrolling issues with your UITableViewCell subclass is that the reuseIdentifier is not matching and so cells aren't being reused. Make sure the reuseIdentifier that you are using in your cellForRowAtIndexPath: method matches the reuseIdentifier that you are setting in Interface Builder for the custom UITableViewCell nib. I had the same problem when I made my first subclass and just matching the reuseIdentifier made everything better. :)

As far as using a UILongPressGestureRecognizer, take a look at the documentation for UIGestureRecognizers and you should be able to figure it out pretty quickly.

UILongPressGestureRecognizer Documentation

UIGestureRecognizer Documentation

Matthew Leffler
  • 1,386
  • 1
  • 19
  • 36
  • Thanks, but I'm not using IB for anything, everything is created programmatically. The problem is a known one, where adding multiple subviews to a cell without custom drawing slows down scrolling dramatically. So I ended up just avoiding subclassing and just "dumbing down" my table view cells and custom drawing/placing pieces of the cell without adding subviews and everything scrolls smoothly. – Matt Nov 10 '10 at 18:55