2011-05-28 16 views
5

En mi UITableView, cuando ingresa al modo de edición, solo me gustaría seleccionar unas pocas celdas. Sé que la clase UITableView tiene la propiedad allowsSelectionDuringEditing, pero esto se aplica a todo UITableView. No veo ningún método delegado relevante para configurar esto por celda.UITableView modo de selección por celda durante la edición

La mejor solución que se me ocurre es establecer allowsSelectionDuringEditing en YES. Luego, en didSelectRowAtIndexPath, filtre las selecciones no deseadas si la vista de tabla está editando. Además, en cellForRowAtIndexPath, cambie esas celdas selectionStyle a Ninguna.

El problema con esto es que pasar al modo de edición no recarga el UITableViewCells, por lo que su selectionStyle no cambia hasta que se desplazan fuera de la pantalla. Entonces, en setEditing, también tengo que iterar sobre las celdas visibles y establecer su selectionStyle.

Esto funciona, pero me pregunto si hay una solución mejor/más elegante para este problema. El esquema básico de mi código está adjunto. ¡Cualquier sugerencia muy apreciada! Gracias.

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

    if (self.editing && ![self _isUtilityRow:indexPath]) return; 
    // Otherwise, do the normal thing... 
} 

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 

    // UITableViewCell* cell = ... 

    if (self.editing && ![self _isUtilityRow:indexPath]) 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    else 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

    return cell; 
} 

- (void) setEditing:(BOOL)editing animated:(BOOL)animated { 

    [super setEditing:editing animated:animated]; 

    if (editing) 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      } 
     } 
    } 
    else 
    { 
     for (UITableViewCell* cell in [self.tableView visibleCells]) 
     { 
      if (![self _isUtilityRow:[self.tableView indexPathForCell:cell]]) 
      { 
       cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      } 
     } 
    } 
} 
+0

Puede volver a cargar la tabla cuando entra en modo de edición, o anular la selección tan pronto como se seleccione en lugar de establecer el estilo de selección en ninguno ... – AMayes

Respuesta

0

No estoy seguro de cómo funciona su aplicación, pero tal vez podría intentar hacer uso de los siguientes en algún lugar de su definición de origen de datos:

// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. 

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

al pasar al modo de edición, utilice la función de filtrar el primer nivel de selección, luego a su segundo nivel de selección

+0

Esto debería funcionar. – CW0007007

Cuestiones relacionadas