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.**
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];
}
You can try adding the gesturerecognizer to the tableviewcell, instead of the tableview. UITableViewCells derive from UIView, as such they can accept gesture recognizers.
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With