/ / Eliminar y agregar fila de UITableView - object-c, uitableview, ios7

Eliminar y agregar la fila UITableView - objetivo-c, uitableview, ios7

En mi aplicación, tengo un UITableView que especifico en - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section Tener 150 filas. Ahora, mi fuente de datos NSArray tiene más de 150 elementos, por lo que, como se esperaba, mi tabla muestra los primeros 150. También implementé - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath para que el usuario pueda borrar elementos de laUITableView. Lo que quiero que ocurra, cuando un usuario ha eliminado una fila, es que la fila eliminada desaparezca (con animación), luego haga que el resto de las filas se desplacen hacia arriba y el elemento 151 de la matriz de origen de datos, que anteriormente no estaba mostrado, para mostrarse como el último elemento de la tabla. Sin embargo, todo lo que he intentado hasta ahora me ha dado el siguiente error:

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 (150) must be equal to the number of rows contained in that section before the update (150), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)."
First throw call stack:
(0x2e58fecb 0x38d2ace7 0x2e58fd9d 0x2ef3de2f 0x30f94761 0x30ff0f7f 0x2af0f49 0xdf763 0x30fba567 0x30fba4f9 0x30df66a7 0x30df6643 0x30df6613 0x30de1d5b 0x30df605b 0x30db9521 0x30df1305 0x30df0c2b 0x30dc5e55 0x30dc4521 0x2e55afaf 0x2e55a477 0x2e558c67 0x2e4c3729 0x2e4c350b 0x334326d3 0x30e24871 0x106b59 0x39228ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

El siguiente código explica la esencia de cómo estoy tratando de implementar esto:

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

if (editingStyle == UITableViewCellEditingStyleDelete) {

PNObject *objectToDelete = contentArray[indexPath.row];
NSMutableArray *mutableCopy = [contentArray mutableCopy];
[mutableCopy removeObject:objectToDelete];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

Respuestas

1 para la respuesta № 1

No sé si aún es relevante el compañero,
Pero lo que creo que está sucediendo es que en realidad no actualiza su fuente de datos.

Está creando una copia mutable "local" de su origen de datos y eliminando el objeto de esa copia, pero su origen de datos real sigue siendo el mismo y aún contiene el elemento que está eliminando.

Supongo que tu numberOfRows:inSection: contiene alguna variación de [contentArray count]

Entonces, lo que sucede es que está "eliminando" un elemento de la vista de tabla, por lo que su aplicación exceptúa esa vista de tabla para tener un elemento menos,
Pero al volver a cargar el contenido de la vista de tabla, ya que el elemento no se eliminó del origen de datos, la vista de tabla tiene un elemento más de lo que esperaba.

Para resolver eso, cambie contentArray a NSMutableArray,
Y en su código anterior, eliminar objectToDelete directamente desde allí.
Luego, también puede eliminar el objeto directamente de la matriz, sin crear una referencia local a él. Por lo que su código de arriba debe verse así:

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

if (editingStyle == UITableViewCellEditingStyleDelete) {

[contentArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}