Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a user has pressed on a UITableViewCell for 2 seconds?

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.**
like image 486
Sheehan Alam Avatar asked Jul 23 '10 00:07

Sheehan Alam


3 Answers

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];
}
like image 107
Steve Avatar answered Nov 13 '22 23:11

Steve


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

like image 29
Joshua Weinberg Avatar answered Nov 14 '22 00:11

Joshua Weinberg


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

}

like image 1
David Wisnowski Avatar answered Nov 13 '22 23:11

David Wisnowski