2011-05-29 26 views
12

He personalizado una UITableViewCell y quiero implementar "deslizar para eliminar". Pero no quiero el botón de eliminación predeterminado. En cambio, quiero hacer algo diferente. ¿Cuál sería la forma más fácil de implementar esto? ¿Hay algunos métodos que se llaman cuando el usuario se desliza para eliminar una celda? ¿Puedo evitar que aparezca el botón de eliminación predeterminado?Cómo detectar un gesto de deslizar para borrar en una UITableviewCell personalizada?

En este momento creo que debo implementar mi propia lógica para evitar el botón de eliminación predeterminado y reducir las animaciones que se producen al deslizar para eliminar en la implementación predeterminada de UITableViewCell.

¿Tal vez tengo que usar un UIGestureRecognizer?

Respuesta

15

Si desea hacer algo completamente diferente, agregue un UISwipeGestureRecognizer a cada celda de tabla vista.

// Customize the appearance of table view cells. 
- (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]; 
    } 

    // Configure the cell. 


    UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)]; 
    [sgr setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [cell addGestureRecognizer:sgr]; 
    [sgr release]; 

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 
    // ... 
    return cell; 
} 

- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer { 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view; 
     NSIndexPath* indexPath = [self.tableView indexPathForCell:cell]; 
     //.. 
    } 
} 
+0

respuesta de vmanjz es mucho mejor debido a que no tener que crear 'UISwipeGestureRecognizer 'para cada celda de la tabla. Al igual que con una mesa muy grande, puede ver un retraso importante creando muchos reconocedores de gestos. – Baza207

+0

puede agregar el reconocedor de gestos a la vista de tabla en su lugar. echa un vistazo a mi respuesta a un problema similar: http://stackoverflow.com/a/4604667/550177 – Felix

+0

Un problema con este enfoque es que solo reconoce el estado .Ended, no el estado .Began –

13

Aquí son dos métodos que se pueden utilizar para evitar el botón de la cancelación:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 

y

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
+0

¡Gracias! ¡Funcionó perfectamente! :) – An1Ba7

+5

También hay - (void) tableView: (UITableView *) tableView didEndEditingRowAtIndexPath: (NSIndexPath *) indexPath si el usuario no elimina la fila – Borzh

Cuestiones relacionadas