5

I am using gesture recognizers:

Initialize in viewDidLoad:

UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
 [self.view addGestureRecognizer:longPressRecognizer];

This is what longPress looks like:

- (void)longPress:(UILongPressGestureRecognizer*)gestureRecognizer {
 if (gestureRecognizer.minimumPressDuration == 2.0) {
  NSLog(@"Pressed for 2 seconds!");
 }
}

How can I tie this into?

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

How will didSelectRowAtIndexPath get a reference to gestureRecognizer.minimumPressDuration?

Basically what I'm trying to achieve is:

**If a user clicks on a cell, check to see if the press is 2 seconds.**
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

3 Answers3

3

Try adding it to the UITableViewCell instead of the UITableView by providing the tableView:willDisplayCell:forRowAtIndexPath: method like so:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [cell addGestureRecognizer:longPressRecognizer];
}
Steve
  • 31,144
  • 19
  • 99
  • 122
2

You can try adding the gesturerecognizer to the tableviewcell, instead of the tableview. UITableViewCells derive from UIView, as such they can accept gesture recognizers.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
1

add a the indexpath.row to the tableviewcell's tag

- (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];
}
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(rowButtonAction:)];
[cell setTag:indexPath.row];
[cell addGestureRecognizer:longPressRecognizer]; 
[longPressRecognizer release];

// Configure the cell...
Group *gp = [_currentItemArray objectAtIndex:indexPath.row];
cell.textLabel.text = gp.name;


return cell;

}

and then access that tag in the longPress by using [[gestureRecognizer view] tag] (in my code its the part with myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]];

-(IBAction)rowButtonAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded) {
    GroupsAdd_EditViewController *myModalViewController = [[[GroupsAdd_EditViewController alloc] initWithNibName:@"GroupsAdd_EditViewController" bundle:nil] autorelease];
    myModalViewController.titleText = @"Edit Group";
    myModalViewController.context = self.context;
    myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]];
    [self.navigationController presentModalViewController:myModalViewController animated:YES];
}

}