UIGestureRecognizer and UITableViewCell problem

I am binding a UISwipeGestureRecognizer to a UITableViewCell to the cellForRowAtIndexPath: method, for example:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)]; gesture.direction = UISwipeGestureRecognizerDirectionRight; [cell.contentView addGestureRecognizer:gesture]; [gesture release]; } return cell; } 

However, the didSwipe method always gets called twice on a successful hit. At first, I thought this was because the gesture starts and ends, but if I exit the GestureRecognizer gesture itself, they are both in the Finished state:

 -(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { NSLog(@"did swipe called %@", gestureRecognizer); } 

Console:

 2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right> 2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right> 

I really really don't know why. I tried obviously checking the state of Ended, but that won't help, as they both come in as "Ended" anyway ... Any ideas?

+43
ios iphone uitableview ipad uigesturerecognizer
Jan 05 2018-11-11T00:
source share
3 answers

Instead of directly adding a gesture recognizer to the cell, you can add it to the table in viewDidLoad .

In the didSwipe -Method method, you can define the affected IndexPath and cell as follows:

 -(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView]; NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; // ... } } 
+109
Jan 05 2018-11-11T00:
source share

He will work with the application delegate.

 - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath { // code } 
0
Feb 18 '12 at 10:08
source share

I had the same problem and solved it by noting the “Scroll Attribute” in the table view attributes.

I don’t need to scroll in my table view, so it doesn’t affect the application in any other way, except that now I don’t get the first response after a gesture transition.

0
Mar 22 '14 at 11:59
source share



All Articles