6

After a long but all in vain search, I am unable to detect the double tap / touch event in my tableview , actually want to call the detail view on double tap on any TableViewCell and in reality I don't even know how to do it at all .

This is my code so far…

In viewDidLoad

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.myTable addGestureRecognizer:tapGesture];

the handleTapGesture method is

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}

and finally on touching or tapping the cell of tableview the delegate method is

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag == true)
    {
        DetailInvoicing *detail = [[DetailInvoicing alloc] initWithNibName:@"DetailInvoicing" bundle:nil];
        detail.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        detail.customerName = [customerArray objectAtIndex:indexPath.row];
        [self presentViewController:detail animated:YES completion:nil];
    }
}

If I remove this flag condition new view is called on just single touch. where am I wrong or is there any other way to do it .

mergenchik
  • 1,129
  • 16
  • 27
M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
  • Please replace `flag = true;` with `flag = YES;` and replace `flag == true` with just `flag`. – nhgrif Mar 15 '14 at 12:31
  • 1
    Why not present the view right where you detect the double-tap (in handleTapGesture)? –  Mar 15 '14 at 12:49
  • @nhgrif it works but not specifically on double tap… when i double tap nothing happened and then it called new view on third and some times on fourth tap … – M Zubair Shamshad Mar 15 '14 at 12:53
  • @Anna i have to send the cell value to new view thats why using this way – M Zubair Shamshad Mar 15 '14 at 12:54
  • @Zaibi, you can figure out the indexPath of the cell that was tapped in the gesture recognizer method itself. See http://stackoverflow.com/questions/3924446/long-press-on-uitableview/3924965#3924965 for an example. –  Mar 15 '14 at 12:57
  • @Anna thanks a lot, this stack overflow link works for me.. thanks again. – M Zubair Shamshad Mar 15 '14 at 13:07
  • @Anna and also your suggestion to call new view in (in handleTapGesture) is great. .. thanks – M Zubair Shamshad Mar 15 '14 at 13:09
  • @Zaibi, May I suggest you post your updated code as an Answer to close this question. Later, you'll be able to accept it. –  Mar 15 '14 at 13:10
  • You can refer this link, hope your problem will be solved. http://stackoverflow.com/questions/1031254/how-can-i-detect-a-double-tap-on-a-certain-cell-in-uitableview Thanks – Harunmughal Mar 15 '14 at 12:47

3 Answers3

11

There is a simple method, without using UITapGestureRecognizer nor implementing custom table view class.

@implementation MyTableViewController
NSTimeInterval lastClick;
NSIndexPath *lastIndexPath;

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSTimeInterval now = [[[NSDate alloc] init] timeIntervalSince1970];
    if ((now - lastClick < 0.3) && [indexPath isEqual:lastIndexPath]) {
        // Double tap here
        NSLog(@"Double Tap!");
    }
    lastClick = now;
    lastIndexPath = indexPath;
}
@end

It is just view lines of code.

greg
  • 4,843
  • 32
  • 47
mergenchik
  • 1,129
  • 16
  • 27
6

Corrected For Swift 3.1

var lastClick: TimeInterval
var lastIndexPath: IndexPath? = nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let now: TimeInterval = Date().timeIntervalSince1970
    if (now - lastClick < 0.3) &&
        (lastIndexPath?.row == indexPath.row ) {
         print("Double Tap!")
    }
    lastClick = now
    lastIndexPath = indexPath
}
Ali Pishvaee
  • 303
  • 5
  • 7
-1

Try like this, in your customtableview class implement this below method:-

 - (void)touchesEnded:(NSSet *)touches 
    withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
  // below numberofclick in integer type
   self.numberOfClicks = [aTouch tapCount];
 [super touchesEnded:touches withEvent:event];
 }

Now in your tableview delegate method (didSelectRowAtIndexPath). And then detect the numberOfclicks whether it is 2 or 1 and modified your condition accordingly. Below is the line of code which you can put in your delegate method to detect the number of taps pressed

   if (myCell.numberOfClicks == 2) {
   NSLog(@"Double clicked");   }}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56