2012-09-14 18 views
10

Sé que una subclase de UITableViewCell puede implementar willTransitionToState y ejecutar código personalizado en el momento de la transición. Pero, ¿hay alguna forma de encontrar el estado actual de una celda?Determinación del estado actual de una celda

En caso negativo, ¿debo clasificar UITableViewCell y definir una propiedad currentState, que siempre actualizo en mi willTransitionToState? Siempre tendré una manera de conocer el estado de cualquier celda en particular.

Parece extraño que no pueda preguntarle a una celda cuál es su estado actual (0, 1, 2 o 3).

Respuesta

15

los estados actuales son UITableViewCellStateDefaultMask (0), UITableViewCellStateShowingEditControlMask (1), UITableViewCellStateShowingDeleteConfirmationMask (2), y UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask (3).

Estos estados corresponden a los valores de las propiedades editing y showingDeleteConfirmation. Se puede probar la siguiente manera:

if (!cell.editing && !cell.showingDeleteConfirmation) { 
    // 0 - UITableViewCellStateDefaultMask 
} else if (cell.editing && !cell.showingDeleteConfirmation) { 
    // 1 - UITableViewCellStateShowingEditControlMask 
} else if (!cell.editing && cell.showingDeleteConfirmation) { 
    // 2 - UITableViewCellStateShowingDeleteConfirmationMask 
} else if (cell.editing && cell.showingDeleteConfirmation) { 
    // 3 - UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask 
} 
+1

Muchos Gracias. Mi objetivo final es identificar las celdas que muestran la confirmación de eliminación porque la celda se pasó (y no porque se utilizó el control de edición). Al mirar estas opciones, parece que no será lo suficientemente probado, y en ambos casos el estado es (3) (la celda y la tabla son ambas 'editoras 'en ambos escenarios). De vuelta al tablero de dibujo. –

8

Para iOS 6, aquí está mi solución:

funcione para cualquiera de los estados de transición y maneja el golpe eliminar gesto así. Coloque este código en su subclase de UITableviewCell.

- (void)willTransitionToState:(UITableViewCellStateMask)state { 

    [super willTransitionToState:state]; 

    if (state == UITableViewCellStateDefaultMask) { 

     NSLog(@"Default"); 
     // When the cell returns to normal (not editing) 
     // Do something... 

    } else if ((state & UITableViewCellStateShowingEditControlMask) && (state & UITableViewCellStateShowingDeleteConfirmationMask)) { 

     NSLog(@"Edit Control + Delete Button"); 
     // When the cell goes from Showing-the-Edit-Control (-) to Showing-the-Edit-Control (-) AND the Delete Button [Delete] 
     // !!! It's important to have this BEFORE just showing the Edit Control because the edit control applies to both cases.!!! 
     // Do something... 

    } else if (state & UITableViewCellStateShowingEditControlMask) { 

     NSLog(@"Edit Control Only"); 
     // When the cell goes into edit mode and Shows-the-Edit-Control (-) 
     // Do something... 

    } else if (state == UITableViewCellStateShowingDeleteConfirmationMask) { 

     NSLog(@"Swipe to Delete [Delete] button only"); 
     // When the user swipes a row to delete without using the edit button. 
     // Do something... 
    } 
} 
+0

En iOS 8.1, el SDK (estado == UITableViewCellStateShowingDeleteConfirmationMask) debe cambiarse a (estado & UITableViewCellStateShowingDeleteConfirmationMask) ya que las posiciones de bit más altas son por cualquier razón que no sea cero. – Neil

0

La versión rápida de jhilgert00 con el comentario de Neil aplicados:

override func willTransitionToState(state: UITableViewCellStateMask) { 

    super.willTransitionToState(state) 

    if (state == UITableViewCellStateMask.DefaultMask) { 
     println("default") 
    } else if (state & UITableViewCellStateMask.ShowingEditControlMask != nil) 
     && (state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil) { 
      println("Edit Control + Delete Button") 
    } else if state & UITableViewCellStateMask.ShowingEditControlMask != nil { 
     println("Edit Control Only") 
    } else if state & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil { 
     println("Swipe to Delete [Delete] button only") 
    } 
} 
0

A partir de rápida 3 el valor de estado es un optionset, se puede utilizar de esta manera:

override func willTransitionToState(state: UITableViewCellStateMask) { 
    super.willTransitionToState(state) 
    if state.contains(.DefaultMask) { 
     print("DefaultMask") 
    } 
    if state.contains(.ShowingEditControlMask) { 
     print("ShowingEditControlMask") 
    } 
} 
Cuestiones relacionadas