2012-10-06 135 views
0
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    //add code here for when you hit delete 

    Budget *deletedBudget = (Budget *)[self.budgetList objectAtIndexPath:indexPath]; 
    [self.context deleteObject:deletedBudget]; 

    NSError* error; 
    if(![self.context save:&error]) { 
     NSLog(@"Failed to save to data store: %@", [error localizedDescription]); 
     NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey]; 
     if(detailedErrors != nil && [detailedErrors count] > 0) { 
      for(NSError* detailedError in detailedErrors) { 
       NSLog(@" DetailedError: %@", [detailedError userInfo]); 
      } 
     } 
     else { 
      NSLog(@" something wierd happens ... hmmm"); 

     } 
    }else { 
     NSLog(@"budget deleted!!!! YEEEEE"); 
    } 



    [self updateBugetList]; 
     [self.tableView beginUpdates]; 
    if ([self.budgetList.fetchedObjects count] >= 1) { 

     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 

    if ([self.budgetList.fetchedObjects count] == 0) { 
     //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
     //[self.tableView reloadData]; 
    } 


    [self.tableView endUpdates]; 




} 

此代碼適用於大多數行,但不適用於最後一行。uitableview刪除行

當刪除最後一行被竊聽的實體被刪除顯示日誌消息,但該行不會從表視圖中刪除...

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 (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).' 

這又如何避免?

回答

0

我困惑這個條件一點點:

if ([self.budgetList.fetchedObjects count] >= 1) { 
... 

     if ([self.budgetList.fetchedObjects count] == 0) { 
...    
     [tableView reloadData]; 
     } 
     [tableView endUpdates]; 
    } 

好像你永遠不會得到更新表,因爲第二個條件將不會運行。 我想你應該在這種情況下

+0

總是重裝表以及事情是...我刪除所有的行,除了後一個...我需要刪除的節..沒有????這就是問題的所在......最後一排仍顯示它的數據從核心數據,並更新了fetchedResults控制器刪除後... – user1028028

+0

你重寫代碼,我建議? – NeverBe

+0

是的,現在我得到一個NSInternalInconsistency異常 – user1028028