2013-12-09 55 views
0

如果所有筆記都在一個部分中,我可以從tableView中刪除項目。 當我將項目添加到多個部分,從另一個部分刪除音符崩潰出錯的應用程序:在多個部分崩潰的tableview中刪除行

'Invalid update: invalid number of rows in section 0. The number of rows contained in an 
existing section after the update (0) 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, 0 deleted) and plus or minus the number of rows moved into or out of 
that section (0 moved in, 0 moved out).' 

有誰知道原因或解決方案呢?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (editingStyle == UITableViewCellEditingStyleDelete) { 

    // Delete the row from the data source 
    [self.tableView beginUpdates]; 
    [self.notes removeObjectAtIndex:indexPath.row]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ]; 

    [self.data writeToFile:self.path atomically:YES]; 
    [self.tableView endUpdates]; 

} 
else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 

} 
+0

這個問題是不是在的cellForRowAtIndexPath連接到一個約一節:我看看面前?你使用 - 如果該部分和NSP在該方法預測? – Greg

+0

是的,他們已連接。我得到了cellForIndexPath與NSPreedicate工作,它將項目添加到正確的部分,但我不使用NSPredicate刪除行 – user3001526

+0

如果重新排列您的數據源數組以反映部分/行結構,例如建議這裏http://stackoverflow.com/questions/20454366/number-of-rows-in-section#comment30562892_20454366或在這裏http://stackoverflow.com/a/20217199/1187415。 –

回答

0

如果您在使用的cellForRowAtIndexPath謂:那麼當您刪除陣列數據,因爲indexPath.row不符合您的數組索引使用它。 嘗試類似的東西:

if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [self.tableView beginUpdates]; 
     if (indexPath.section == 0){ 
      NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category =[cd] %@", @"Category 1"]; 
      NSArray * catArray = [self.notes filteredArrayUsingPredicate:pred]; 
      [self.notes removeObject:catArray[indexPath.row]] 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ]; 


     } //else other section .... 

     [self.data writeToFile:self.path atomically:YES]; 
     [self.tableView endUpdates]; 
    } 
+0

請注意,如果數組包含重複元素,'[self.notes removeObject:catArray [indexPath.row]]'可能會刪除錯誤的元素。 –

+0

謝謝,但這給出了理由:'無效的更新:無效的行數在第0部分... – user3001526

+0

並感謝馬丁R,那對我來說目前不是問題 – user3001526

相關問題