UITableView stürzt ab, wenn versucht, löschen Sie eine Zelle, und Abschnitt

Habe ich gearbeitet, auf die Schaffung einer UITableView, dessen Inhalt geladen eine NSUserDefaults-Objekt. Immer wenn ich Versuch zu löschen eine Zelle/Abschnitt, jedoch stürzt das Programm ab und spuckt folgende:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1030
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).

Egal wie viel ich lese, bis auf diese Fehler und versuchen, es zu beheben, ich habe schon stecken hier für die letzten paar Stunden. Hier ist meine Methode, die aufgerufen wird, wenn der Benutzer tippt auf die Schaltfläche "löschen":

 (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"] removeObjectAtIndex:indexPath.row];
        //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"] removeObjectAtIndex:indexPath.row];

        NSMutableArray *rawArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"];
        NSMutableArray *rawDates = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"];

        [rawArray removeObjectAtIndex:indexPath.row];
        [rawDates removeObjectAtIndex:indexPath.row];

        [[NSUserDefaults standardUserDefaults] setObject:rawArray forKey:@"rawArray"];
        [[NSUserDefaults standardUserDefaults] setObject:rawDates forKey:@"rawDates"];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];

    }    
}

und hier sind die Methoden zum bestimmen der Anzahl von Zeilen und Zellen:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

Die Letzte Methode, in der es zurückgibt, weil, wie jetzt, ich Plane, auf nur eine Zelle pro Abschnitt. Vielen Dank für die Zeit nehmen, vorbei zu schauen hier, und ich hoffe wirklich, dass Sie mir helfen können! :/

  • Ich denke, du hast vergessen zu nennen [tableView beginUpdates]; und [tableView endUpdates];
  • Aktualisieren Sie die Datenquelle Modell entsprechend;
InformationsquelleAutor insanj | 2012-04-22
Schreibe einen Kommentar