Tengo un UITableView que presenta algunas configuraciones para el usuario. Algunas celdas están ocultas a menos que un UISwitch esté en la posición 'On'. Tengo el siguiente código:Mostrar y ocultar UITableViewCell con UISwitch bloqueos demasiado rápidos
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return switchPush.on ? 6 : 1;
}
// Hooked to the 'Value Changed' action of the switchPush
- (IBAction)togglePush:(id)sender {
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:0];
for(int i = 1; i <= 5; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[tableView beginUpdates];
if(switchPush.on) {
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
}
Esto funciona como se espera, hasta que me cambio la UISwitch dos veces en rápida sucesión (pulsando dos veces), en cuyo caso la aplicación se bloquea con un
Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.
Sé que es causado por el valor de retorno incorrecto de numberOfRowsInSection ya que el interruptor está de regreso en su posición original mientras la animación de la celda todavía se está reproduciendo. Intenté desactivar el alternar y conectar el código con otros controladores de eventos, pero nada parece evitar el bloqueo. El uso de reloadData en lugar de la animación también resuelve el problema, pero preferiría las agradables animaciones.
¿Alguien sabe una forma correcta de implementar esto?
¿Qué es un ITUoggle? ¿Te refieres a un 'UISwitch'? – Mundi
Sí lo hice, corregí la pregunta. –