2012-01-30 13 views

Respuesta

6

No he intentado e implementado esto, pero lo intentaré. En primer lugar, crear una costumbre UITableViewCell, y dejar que se tienen 2 propiedades que se pueden utilizar

  1. Una referencia a la tableView que se está utilizando en.
  2. Un indexPath para encontrar la celda de la tableView. (Nota, esto debe actualizarse cada vez que elimine una celda para todas las celdas donde esto cambie)

En su cellForRowAtIndexPath:, donde crea la celda personalizada, establezca estas propiedades. También añadir un UISwipeGestureRecognizer a la célula

cell.tableView=tableView; 
cell.indexPath=indexPath; 
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
[cell addGestureRecognizer:swipeGestureRecognizer]; 
[swipeGestureRecognizer release]; 

Asegúrese de que el gesto sólo recibe golpes horizontales.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
     ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
     ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
} 

En su deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec 
{ 
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
    CustomCell *cell=[swipeGestureRecognizer view]; 
    UITableView *tableView=cell.tableView; 
    NSIndexPath *indexPath=cell.indexPath; 
    //you can now use these two to perform delete operation 
} 
+0

No olvide que debe establecer la dirección del reconocedor de gestos cuando lo inicialice por primera vez para agregarlo a la celda. Algo como esto: swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris

+0

Apple, maldito sea por no proporcionar '(void) cancelPreviousPerformRequestsWithTarget: (id) aSelector de destino: (SEL) aSelector'. –

-2

para los que tiene que agregar este código en su proyecto.

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
    }  
} 

información Else mor u puede ir a través de este enlace enter link description here

+0

Vivek2012 y iWill: gracias por sus respuestas, pero no quiero mostrar un botón ELIMINAR, ya hay respuestas a las de stackoverflow. Quiero que la celda se borre desde el momento en que recibe un gesto hacia la izquierda o hacia la derecha.lea mi descripción gracias – carbonr

-1

implemento

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

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

estos dos métodos, si el UITableViewCellEditingStyle es UITableViewCellEditingStyleDelete, a continuación, hacer algo para borrar su celda con

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

eso es todo.

3

La solución Publicado por @MadhavanRP funciona, pero es más complejo de lo que debe ser. Podría adoptar un enfoque más simple y crear un reconocedor de gestos que maneje todos los deslizamientos que ocurren en la tabla, y luego obtener la ubicación del deslizamiento para determinar qué celda usó el usuario.

Para configurar el reconocedor gesto:

- (void)setUpLeftSwipe { 
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                 action:@selector(swipeLeft:)]; 
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.tableView addGestureRecognizer:recognizer]; 
    recognizer.delegate = self; 
} 

llama a este método en viewDidLoad

Para manejar el golpe:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    ... do something with cell now that i have the indexpath, maybe save the world? ... 
} 

nota: su vc debe implementar el delegado gesto reconocedor .

Cuestiones relacionadas