Tengo una UITableView que muestra una lista de objetos almacenados con CoreData. Puedo eliminar un objeto usando el siguiente código:Animación de eliminación de fila en UITableView con CoreData da error de aserción
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Delete row");
[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![managedObjectContext save:&error]) {
/*do this gracefully one day */
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self refreshTables]; //where refreshTables just reloads the data the table is using and calls [self.tableView reloadData];
}
}
Pero no tiene animación ni estética.
Cuando intento para animar al reemplazar
[self refreshTables];
con
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
me sale el siguiente error:
Assertion failure in -[UITableView _endCellAnimationsWithContext:], >/SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:920 2010-10-30 16:46:35.717 MyApp[38226:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
He intentado tener los código en deleteRowsAtIndexPaths una variedad de lugares en el código commitEditingStyle sin lu ck (por ejemplo, antes de eliminar el objeto del mOC) pero parece que no puedo evitar este error.
Sé que el ejemplo de iPhoneCoreDataRecipes de Apple maneja el problema configurando un delegado para FetchedResultsController para manejar las filas de edición/eliminación, pero en esta etapa del desarrollo, si es posible, solo quiero una solución simple para animar esos objetos eliminados.
¿Cómo puedo animar la eliminación de una fila, antes/después de eliminar el objeto de mi managedObjectContext?
EDITAR: He intentado tener deleteRowsAtIndexPaths antes y después de eliminar el elemento del mOC, con el mismo error.
# 2 solucionó mi problema. ¡Gracias! –