Search code examples
iosuigesturerecognizerruntime-erroruiswipegesturerecognizer

Adding a UIGestureRecognizer to a UITableViewCell


I used http://www.raywenderlich.com/21842/how-to-make-a-gesture-driven-to-do-list-app-part-13 to add a gesture recognizer to my cells, so that i can move them from left to right, but i get this error:

[UISwipeGestureRecognizer translationInView:]: unrecognized selector

I dont even use a swipe recognizer so i dont know what to do now.

UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];

Thanks!


Solution

  • It would appear that your gestureRecognizerShouldBegin is being called for a gesture other than the UIPanGestureRecognizer you created. Clearly, you could solve the symptom by just changing your gestureRecognizerShouldBegin in your cell subclass to test to see if it's a pan gesture:

    -(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
    {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
        {
            CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    
            if (fabsf(translation.x) > fabsf(translation.y)) {
                return YES;
            }
            return NO;
        }
    
        return NO;
    }
    

    The above fix just addresses the symptom, though. The real question is how your UIGestureRecognizerDelegate method, gestureRecognizerShouldBegin, is being called for gestures other than your UIPanGestureRecognizer. There can be other gestures in play (e.g., if you've implemented a canEditRowAtIndexPath that returns YES, that enables a swipe gesture), but I don't see how your custom cell would be recognized as the delegate for that swipe gesture.

    The only way I can reproduce your problem is by manually adding a swipe gesture to the cell (or table or view). I know you say you didn't use a swipe gesture, but maybe one was accidentally added in Interface Builder or something like that? I don't know because I'm having trouble otherwise reproducing the symptom you describe. If worst comes to worst, you can compress your project and share it with us, and I'd be happy to take a quick look.