Double click on UITableViewCell

I want to get one tap, and also double-tap UITableViewCell. I created customDataSource for UITableview.

How can i achieve this?

+5
source share
4 answers

The proper way to do this is to add your UITapGestureRecognizer to the tableView:

UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:doubleTap];

UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.tableView addGestureRecognizer:singleTap];

Then in your callback methods you will find a cell with something like this:

-(void)singleTap:(UITapGestureRecognizer*)tap
{
    if (UIGestureRecognizerStateEnded == tap.state)
    {
        CGPoint p = [tap locationInView:tap.view];
        NSIndexPath* indexPath = [_tableView indexPathForRowAtPoint:p];
        UITableViewCell* cell = [_tableView cellForRowAtIndexPath:indexPath];
        // Do your stuff
    }
}
+14
source

I achieved this by adding the following to your gesture recognizer, although I found a slight delay on a single tap.

[tapRecognizer setDelaysTouchesBegan:YES];
[tapRecognizer setCancelsTouchesInView:YES];
+6
source

[doubleTap release];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];

doubleTap.numberOfTapsRequired = 2;

doubleTap.numberOfTouchesRequired = 1;

[cell addGestureRecognizer:doubleTap];

[doubleTap release];
0

You need to have this line to separate between single and double labels. [singleTap requireGestureRecognizerToFail: doubleTap]; Here is a sample code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // After cell initialized code
  //...
  //...

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

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

  [cell addGestureRecognizer:singleTap];
  [cell addGestureRecognizer:doubleTap];
  return cell;
}

-(void)doubleTap:(UITapGestureRecognizer*)tap
{
  NSLog(@"Double Taps");

  CGPoint point = [tap locationInView:self.tableView];
  NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint: point];
  UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
  // do your stuff.
  // ...
}

-(void)singleTap:(UITapGestureRecognizer*)tap
{
  NSLog(@"Single Tap");
  CGPoint point = [tap locationInView:self.tableView];
  NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:point];
  UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];

  // Do you stuff with index path or cell here.
  // ...

}
0
source

All Articles