0

I want to perform one action if the user made a single touch and another if a user made a double touch on a UITableView Cell.

I tried multiple approaches mentioned in this question.

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

But every approach, I cannot distinguish single tap and double correctly. What I meant is, in each double tap, it happens a single tap also. So, double tap happens, every time single tap action also fired.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FeedCell *myCell = (FeedCell*) [self.tblView cellForRowAtIndexPath:indexPath];

NSLog(@"clicks:%d", myCell.numberOfClicks);

if (myCell.numberOfClicks == 2) {
    NSLog(@"Double clicked");
}
else{

    NSLog(@"Single tap");
}
}  

What should be the correct way to do this?

Community
  • 1
  • 1
smartsanja
  • 4,413
  • 9
  • 58
  • 106

2 Answers2

2

I would prefer to don't used didSelectRowAtIndexPath while you want double tap action. Used single TapGesture that replace with didSelectRowAtIndexPath. whatever code you wrote in didSelectRowAtIndexPath, that will write in single tap selector method.

Example : Implement Single and Double gesture like as below.

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1; 
[self.view addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2; 
[self.view addGestureRecognizer:doubleTap];

[singleTap requireGestureRecognizerToFail:doubleTap];
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
0

According to the answer - your single tap will handle on timer fire method. Put your single tap action here

- (void)tapTimerFired:(NSTimer *)aTimer{
    //timer fired, there was a single tap on indexPath.row = tappedRow
    if(tapTimer != nil){
        tapCount = 0;
        tappedRow = -1;
    }
}

and double-tap will handle in didSelectRowAtIndexPath exactly as showed:

- (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];
    }
    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];
        }
    }
}

You just need to declare two properties

@property (nonatomic, assign) NSInteger tapCount;
@property(nonatomic, assign) NSInteger tappedRow;

This is fully working snippet.

Doro
  • 2,413
  • 2
  • 14
  • 26