5

Ich verwende Geste Erkenner:Wie kann ich feststellen, ob ein Benutzer 2 Sekunden lang eine UITableViewCell gedrückt hat?

initialisieren in viewDidLoad:

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

Dies ist, was longPress wie folgt aussieht:

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

Wie kann ich dies in binden?

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

Wie wird DidSelectRowAtIndexPath einen Verweis auf gestureRecognizer.minimumPressDuration bekommen?

Im Grunde, was ich versuche zu erreichen ist:

**If a user clicks on a cell, check to see if the press is 2 seconds.** 

Antwort

3

Versuchen Sie, auf die UITableViewCell anstelle des UITableView Zugabe von der Tableview bietet: willDisplayCell: forRowAtIndexPath: Methode wie folgt:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell addGestureRecognizer:longPressRecognizer]; 
}
2

Sie können versuchen, die GestureRecognizer zum tableviewcell Zugabe anstelle des Tableview. UITableViewCells werden von UIView abgeleitet und können daher Gestenerkenner akzeptieren.

+0

Können Sie mir in Code? –

+1

Steve scheint genau das getan zu haben. –

1

eine der indexpath.row auf die tableviewcell des Tags hinzufügen

- (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; 

}

und dann Zugriff auf dieses Tag in der LongPress mit dem [[GestenRecognizer View] Tag] (in meinem Code ist der Teil mit 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]; 
} 

}