Cell aus TableView löschen führt zum Absturz

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Cell aus TableView löschen führt zum Absturz

    Hallo,

    bei meiner aktuellen App nutze ich Core Data und mehrere TableViewController hintereinander. Erste TableView zeigt alle Objekte der Entität A an. Ein A hat mehrere Entitäten B, die im zweiten ViewController angezeigt werden.
    Und so geht es mit noch einpaar weiteren Entitäten und TableViews weiter. So eine Art Verwaltung.

    Ich habe nun das Problem, dass ich nur die Objekte der ersten TableView löschen kann ohne einen Fehler zu bekommen. Bei allen anderen TableViews/Entities bekomme ich folgenden Fehler:
    *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:1330
    *** 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 (1) must be equal to the number of rows contained in that section before the update (1), 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).'

    Weiß jemand warum dieser Fehler kommt?

    Quellcode

    1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    2. {
    3. // Return the number of sections.
    4. return [[self.fetchedResultsController sections] count];
    5. }


    Quellcode

    1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    2. {
    3. if (editingStyle == UITableViewCellEditingStyleDelete) {
    4. // Delete the row from the data source
    5. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    6. }
    7. else if (editingStyle == UITableViewCellEditingStyleInsert) {
    8. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    9. }
    10. }


    Würde mich über Vorschläge freuen!
    Gruß
  • Hi

    Danke für deine Antwort.

    Ich bin grad selbst drauf gekommen, dass ich sie GAR NICHT lösche :rolleyes:

    Hier die Korrektur und nun funktionierts:

    Quellcode

    1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    2. {
    3. if (editingStyle == UITableViewCellEditingStyleDelete) {
    4. NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    5. [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
    6. NSError *error = nil;
    7. if (![context save:&error]) {
    8. // Replace this implementation with code to handle the error appropriately.
    9. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    10. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    11. abort();
    12. }
    13. }
    14. else if (editingStyle == UITableViewCellEditingStyleInsert) {
    15. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    16. }
    17. }
    Alles anzeigen