2011-08-22 11 views

Respuesta

53

Así que lo que necesita es esencialmente conseguir la célula a partir gesto de golpe sobre la mesa? Correcto. No necesitas saber el indexPath. En primer lugar definir golpe en la tableView como tal -

UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipe:)]; 
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight; 
[tableView addGestureRecognizer:showExtrasSwipe]; 
[showExtrasSwipe release]; 

Después de esto, cuando el golpe real ocurre que necesita para poner manejador a ella. Para probar esto: que

-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture 
{ 
    CGPoint location = [gesture locationInView:tableView]; 
    NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:location]; 
    UITableViewCell *swipedCell = [tableView cellForRowAtIndexPath:swipedIndexPath]; 

    //Your own code... 
} 

Así que lo que hemos hecho en primer lugar se adjunte una SwipeGestureRecognizer a la UITableView (no el UITableViewCell). Después de eso, cuando el deslizamiento ocurre en el UITableView, primero obtengo las coordenadas de donde ocurrió el gesto en el UITableView. A continuación, usando estas coordenadas obtengo el IndexPath de la fila de donde se produjo el deslizamiento en el UITableView. Finalmente, usando el IndexPath obtengo el UITableViewCell. Simple realmente ..

Nota: Me han preguntado esto demasiadas veces. Así que agregué esta explicación de por qué usé SwipeGestureRecognizer en el UITableView y no en cada individuo UITableViewCell.

Pude haber adjuntado SwipeGestureRecognizer a cada UITableViewCell. No hice eso ya que tendría que adjuntar un SwipeGestureRecognizer por separado para cada celda. Entonces, si tuviera 1000 celdas en mi UITableView, habría tenido que crear 1000 objetos SwipeGestureRecognizer. Esto no está bien. En mi enfoque anterior, simplemente creo uno SwipeGestureRecognizer y eso es todo.

+1

Muchas gracias ... Funcionó. –

+2

por supuesto que sí. ¡Esta pieza de código en particular ya ha visto millones de golpes! –

+0

Creo que necesita cambiar el pinchedIndexPath para swipedIndexPath – DenTheMan

0

Si está intentando implementar 'deslizar para eliminar' (deslizar en dirección horizontal), no necesita volver a inventar la rueda. Simplemente elimine el comentario del método commitEditingStyle:forRowAtIndexPath: en su delegado UITableViewController.

Cuestiones relacionadas