2015-04-23 20 views
5

Pracuję nad projektem przy użyciu Microsoft Azure services. W że podczas usuwania wiersz otrzymuję ten błąd:Nieprawidłowa aktualizacja: nieprawidłowa liczba wierszy w sekcji

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 (2) must be equal to the number of rows 
contained in that section before the update (2), 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). 

kodeks obciążenie stołu i usunąć wiersz jest jako:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 3; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //medicine list 
    if (section == 0) { 
     NSArray *sectionInfo = [self.fetchedResultsController1 fetchedObjects]; 
     return [sectionInfo count]; 

    } 
    //allergy list 
    else if (section == 1) { 
     NSArray *sectionInfo = [self.fetchedResultsController2 fetchedObjects]; 
     return [sectionInfo count]; 

    } 
    //notes list 
    else if (section == 2){ 
     NSArray *sectionInfo = [self.fetchedResultsController3 fetchedObjects]; 
     return [sectionInfo count]; 
    } 
    else 
     return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"mcell"; 
    MedicationTableViewCell *cell = (MedicationTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    // Add utility buttons 
    NSMutableArray *rightUtilityButtons = [NSMutableArray new]; 
    [rightUtilityButtons sw_addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"Delete"]; 

    switch (indexPath.section) { 
     case 0: { 
      NSManagedObject *item = [self.fetchedResultsController1 objectAtIndexPath:indexPath]; 
      cell.textLabel.text = [item valueForKey:@"name"]; 
      break; 
     } 
     case 1: { 
      NSManagedObject *item = [self.fetchedResultsController2 objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]; 
      cell.textLabel.text = [item valueForKey:@"name"]; 
      break; 
     } 
     case 2: { 
      NSManagedObject *item = [self.fetchedResultsController3 objectAtIndexPath: [NSIndexPath indexPathForRow:indexPath.row inSection:0]]; 
      cell.textLabel.text = [item valueForKey:@"title"]; 
      break; 
     } 
     default: 
      break; 
    } 
    cell.rightUtilityButtons = rightUtilityButtons; 
    cell.delegate = self; 

    return cell; 
} 

- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index { 

// Delete button is pressed 
NSIndexPath *cellIndexPath = [self.medicationsTableView indexPathForCell:cell]; 

//fetchingg data from local store first 
NSManagedObject *item = [self.fetchedResultsController1 objectAtIndexPath:[NSIndexPath indexPathForRow:cellIndexPath.row inSection:0]]; 

    NSMutableArray *keys = [[NSMutableArray alloc] initWithObjects:@"id", @"name", @"userId", nil]; 
    NSMutableArray *values = [[NSMutableArray alloc] initWithObjects: [item valueForKey:@"id"], [item valueForKey:@"name"], [item valueForKey:@"userId"], nil]; 

    //creating dictionary of data 
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 

    //calling delete fucntion 
    [self.authService deleteItem:self.syncTable withDict:dict completion:^{ 
    //removing row from table view 
    [self.medicationsTableView reloadData]; 
    [self.medicationsTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight]; 
     }]; 
     break; 
} 

Proszę powiedzieć, gdzie mam zamiar źle. Dzięki za awans !!!!

+0

Musisz usunąć obiekt z self.fetchedResultsController1, zanim usunieszRataAtIndexPaths. – iBhavin

+0

fetchedResultsController w opcji delegate już to robi. Już to sprawdziłem. – Arti

+0

iBhavin próbował, ale wciąż ten sam komunikat o błędzie nadchodzi – Arti

Odpowiedz

16

W - (void)swipeableTableViewCell: didTriggerRightUtilityButtonWithIndex:

Musisz usunąć albo

[self.medicationsTableView reloadData]; 

lub

[self.medicationsTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight]; 

Bo na pierwszej linii, gdy reloadData sprawdzony to przeładować tableview z nowego źródła danych i znowu dzwoni deleteRowsAtIndexPaths próbował aby usunąć wiersze, które zostały wcześniej usunięte przez wywołanie reloadData wcześniej.

11

Ważną rzeczą jest to, że trzeba użyć

[self.medicationsTableView reloadData]; 

lub

[self.medicationsTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight]; 

Powodem jest, gdy tableview's reload data nazywa się wiersz, który jest usuwany ze źródła danych są usuwane i po że gdy api wywołań dla wiersza usuwania jest wywoływane dla tego samego index path (gdzie wiersz jest już usunięty powoduje emisję)

Również s t przed usunięciem wiersz z tabeli usunąć obiekt ze źródła danych (self.fetchedResultsController1) i wywołać

[self.medicationsTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight]; 

i po animacja usunięcie powyższych Row jest zakończona niż można zadzwonić (ale nie jest to wymagane)

[self.medicationsTableView reloadData]; 
+3

@TapasPal to nie ma znaczenia. I jest jakaś różnica między obiema odpowiedziami. sprawdź najpierw –

+3

@TapasPal, może się zdarzyć, że osoba podczas tworzenia odpowiedzi nie zobaczy odpowiedzi, które zostały wysłane, gdy był w trakcie tworzenia odpowiedzi i co najważniejsze, dlaczego miałby dbać o inne osoby, pasują do moich lub podobnych? –

+0

W tym scenariuszu nie ma potrzeby wywoływania 'reloadData', jeśli używamy' deleteRowsAtIndexPaths' –

Powiązane problemy