La aplicación se bloquea con el error SIGABRT al intentar eliminar una fila en tableView

my app crashes when the user tries to delete a row from the UITableView and in the debugger I get the SIGABRT error.

Here is my code for deletion:

- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    [[self displayedObjects] removeObjectAtIndex:[indexPath row]];


    //  Animate deletion
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [[self tableView] deleteRowsAtIndexPaths:indexPaths
                            withRowAnimation:UITableViewRowAnimationFade];
    //[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
  }
}

preguntado el 09 de marzo de 12 a las 16:03

Try deleting the row prior to the object in displayedObjects. -

I think I know what your problem is. I'll post an answer -

Thanks Raphael Ayres I tried your idea but it didn't work. -

4 Respuestas

I got help in another question. And the problem is that the object would be deleted from the array but then before deleting the row from the table view , the numberOfRowsInSection is called again which then loads the array from disk again and give the number before the deletion due to a problem in my loading of the array. I put a break point at the numberOfRowsInSection and it is all clear now.

respondido 17 mar '12, 16:03

¿Tiene su [self displayedObjects]'s item count cover [indexPath row]? For example, your [self displayedObjects] only has 5 item but you want to removeObjectAtIndex:6.

respondido 09 mar '12, 16:03

Yes. I can see the displayedObjects array deleting the right object correctly. it's only when trying to delete the table view row my app crashes. - Ali

I compared with my code to see where you went wrong.

        //Removes From Table
    [favorites removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

Favorites is my array. What I think you're going to need to do is change this line:

   [[self displayedObjects] removeObjectAtIndex:[indexPath row]];

First, put that line immediately before this one:

[[self tableView] deleteRowsAtIndexPaths:indexPaths
                            withRowAnimation:UITableViewRowAnimationFade];

Y luego cambia self to the name of your array that the table is displaying.

ALSO! VERY IMPORTANT

Make sure the array that the table is displaying is a NSMutableArray No un NSArray

With an NSArray, you can't change (add or delete) values. However, with an NSMutableArray you can, so make sure you aren't displaying a NSArray

respondido 09 mar '12, 17:03

I think what you are saying is what Raphael Ayres is saying above in his comment: to try deleting the tableview row before removing the object from the array the tableview is taking its data from. I tried it but it still crashes. - Ali

Did you make sure the array is mutable? - JTApps

Yes. It's mutable. By the way I'm serializing it and then unserializing it which gives an NSArray that I make an NSMutableArray copy of it. I hope this doesn't cause any problem. I see the array deleting one object properly. I think that there is a problem with the row deletion. - Ali

Try copying my code exactly and replacing yours with it (comment yours out), and just change "favorites" with the name of array that the table displays. Comment our your entire section of that method and stick mine in there. Try it out, never know if it'll work - JTApps

Thanks for your help. Still gives the SIGABRT error. Here is the new code - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //Removes From Table [_displayedObjects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } - Ali

If I'm not mistaken, you're calling this code:

 [[self tableView] deleteRowsAtIndexPaths:indexPaths
                            withRowAnimation:UITableViewRowAnimationFade];

from within a committed delete event. I believe all you need to do is update your backing store from the "tableView:commitEditingStyle:forRowAtIndexPath:" method. The delete has already been committed. You may also need refresh your table view afterwards also make sure to mark your table view as allowing updates.

Try commenting out all of this code and tell us what happens:

//  Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self tableView] deleteRowsAtIndexPaths:indexPaths
                        withRowAnimation:UITableViewRowAnimationFade];

respondido 09 mar '12, 17:03

Regarding updating my backing store, I think that's what I'm doing. I'm updating my array called displayedObjects in this line: [[self displayedObjects] removeObjectAtIndex:[indexPath row]]; and it is working properly deleting the right object from the array. I did what you suggested and commented the row deletion. The app won't crash but it won't delete the row until I go to another view and come back to see the change. I tied to reload the table but that didn't help either. It's this line causing the crash but how else would I delete the table row? - Ali

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.