objective ios objective-c arrays nsmutablearray

ios - nsarray objective c



''Actualización no válida: número de filas no válido en la sección 0 (1)

He leído todas las publicaciones relacionadas con esto y todavía tengo un error:

''Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), 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).''

Aquí están los detalles:

en .h tengo un NSMutableArray :

@property (strong,nonatomic) NSMutableArray *currentCart;

En .m mi numberOfRowsInSection ve así:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return ([currentCart count]); }

Para habilitar eliminar y eliminar el objeto de la matriz:

// Editing of rows is enabled - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //when delete is tapped [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [currentCart removeObjectAtIndex:indexPath.row]; } }

¿Pensé que al tener mi número de secciones dependiendo del recuento de la matriz que estoy editando aseguraría el número adecuado de filas? ¿No se puede hacer esto sin tener que volver a cargar la tabla cuando elimina una fila de todos modos?


deleteRowsAtIndexPaths:withRowAnimation: eliminar el objeto de la matriz de datos antes de llamar a deleteRowsAtIndexPaths:withRowAnimation: Entonces, tu código debería verse así:

// Editing of rows is enabled - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //when delete is tapped [currentCart removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } }

También puede simplificar un poco su código usando el método abreviado de creación de matriz @[] :

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];