Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect a double tap on a certain cell in UITableView?

How can I detect a double tap on a certain cell in UITableView?

i.e. I want to perform one action if the user made a single touch and another if a user made a double touch? I also need to know an index path where the touch was made.

How can I achieve this goal?

Thanks.

like image 527
Ilya Suzdalnitski Avatar asked Jun 23 '09 08:06

Ilya Suzdalnitski


People also ask

How will you know the end of loading of UITableView?

willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.

How do I get IndexPath from cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.


2 Answers

Override in your UITableView class this method

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  {       if(((UITouch *)[touches anyObject]).tapCount == 2)     {     NSLog(@"DOUBLE TOUCH");     }     [super touchesEnded:touches withEvent:event]; } 
like image 27
oxigen Avatar answered Sep 21 '22 06:09

oxigen


If you do not want to create a subclass of UITableView, use a timer with the table view's didSelectRowAtIndex:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     //checking for double taps here     if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){         //double tap - Put your double tap code here         [tapTimer invalidate];         [self setTapTimer:nil];          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Double Tap" message:@"You double-tapped the row" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];         [alert show];         [alert release];     }     else if(tapCount == 0){         //This is the first tap. If there is no tap till tapTimer is fired, it is a single tap         tapCount = tapCount + 1;         tappedRow = indexPath.row;         [self setTapTimer:[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimerFired:) userInfo:nil repeats:NO]];     }     else if(tappedRow != indexPath.row){         //tap on new row         tapCount = 0;         if(tapTimer != nil){             [tapTimer invalidate];             [self setTapTimer:nil];         }     } }  - (void)tapTimerFired:(NSTimer *)aTimer{     //timer fired, there was a single tap on indexPath.row = tappedRow     if(tapTimer != nil){         tapCount = 0;         tappedRow = -1;     } } 

HTH

like image 109
lostInTransit Avatar answered Sep 19 '22 06:09

lostInTransit