Jim B
Jim B

Reputation: 2277

Gesture Recognizers and TableView

I have a UIView which covers all of a UITableView. The UIView is using gesture recognizers for control of what the table displays. I still need the vertical UITableView scrolling and row taps. How do I pass these on to the table from the gesture recognizers?

Upvotes: 13

Views: 27484

Answers (3)

jwhat
jwhat

Reputation: 2042

If you need to know your cell's indexPath:

- (void)handleSwipeFrom:(UIGestureRecognizer *)recognizer {
    CGPoint swipeLocation = [recognizer locationInView:self.tableView];
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
    UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
}

This was previously answered in UIGestureRecognizer and UITableViewCell issue.

Upvotes: 30

onnlv
onnlv

Reputation: 151

I tried Rob Bonner's suggestion and it works great. thank you.

But, in my case, there's a problem with direction recognition. (recognizer.direction always refer 3) I'm using IOS5 SDK and Xcode 4.

It seems caused by "[gesture setDirection:(left | right)]" I think. (because predefined (dir left | dir right) calculation result is 3)

So, if someone has problem like me and want to recognize swipe left and right seprately, then assign two recognizer to table view with different directions.

Like this:

UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] 
                                             initWithTarget:self
                                             action:@selector(handleSwipeLeft:)];
[swipeLeftGesture setDirection: UISwipeGestureRecognizerDirectionLeft];

UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] 
                                              initWithTarget:self 
                                              action:@selector(handleSwipeRight:)];

[swipeRightGesture setDirection: UISwipeGestureRecognizerDirectionRight];

[tableView addGestureRecognizer:swipeLeftGesture];
[tableView addGestureRecognizer:swipeRightGesture];

and gesture action below:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    [self moveLeftColumnButtonPressed:nil];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    [self moveRightColumnButtonPressed:nil];
}

I coded with ARC feature then if you are not using ARC, add release codes.

PS: My english is not so good, so if there's any sentential error, correction will be very pleased :)

Upvotes: 7

Rob Bonner
Rob Bonner

Reputation: 9424

Assign your gesture to the table view and the table will take care of it:

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSwipeFrom:)];
[gesture setDirection:
        (UISwipeGestureRecognizerDirectionLeft
        |UISwipeGestureRecognizerDirectionRight)];
[tableView addGestureRecognizer:gesture];
[gesture release];

Then in your gesture action method, act based on the direction:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self moveLeftColumnButtonPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        [self moveRightColumnButtonPressed:nil];
    }
}

The table will only pass you the gestures you have asked for after handling them internally.

Upvotes: 30

Related Questions