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?
-
I believe "[tablView gesture]" should be "[yourTableView addGestureRecognizer:gesture]" – Jun 23 '11 at 19:17
3 Answers
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.
-
Thank you, i was looking for this solution. I had no clue `locationInView` thanks a lot – carbonr Jan 30 '12 at 08:46
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.

- 35,901
- 5
- 77
- 111

- 9,276
- 8
- 35
- 55
-
This is the great answer. But can you please tell me how to know the tableview indexPath in handleSwipeFrom method? – dinesh Jan 20 '12 at 10:14
-
5This won't work - the direction property on UISwipeGestureRecognizer indicates what directions CAN be recognised (in this case left AND right) not what direction WAS swiped. You will need to separate gesture recognisers – Martin Lockett Sep 02 '13 at 10:22
-
1Yes, recognizer.direction will always be 3 and never match. This is arguably a bug: http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer – Symmetric Sep 29 '13 at 03:33
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 :)

- 151
- 1
- 6