2012-08-03 69 views
1

我使用NSFetchedResultsController來填充tableview,並且每件事情都可以(添加deletng ..)但每當我嘗試從tableview中刪除最後一行時(當tablview中只有一行時無法從fetchedResultsController中刪除最後一個對象

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037 
2012-08-03 16:32:39.667 MackaWithCoreData[793:15503] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 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). with userInfo (null) 

據我看到的,後刪除對象時的委託方法嘗試重新加載tableview中,因爲fetchedResultsController爲null,導致系統崩潰。如何處理這個問題:)應用程序與下面的崩潰日誌問題?在此先感謝

編輯controller controllerDidChangeContent:..的實施。方法是低於

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 

    switch(type) 
    { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      XLog(@"the row removed from tv"); 

      break; 

     case NSFetchedResultsChangeUpdate: 
     // [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      XLog(@" object deleted from fetchedresultscontroller"); 

      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 
+0

能否請您告訴我們您的控制器的'執行:didChangeObject:atIndexPath:forChangeType:newIndexPath:' – Daniel 2012-08-03 13:51:59

+0

@Daniel請看看我的編輯 – ilhnctn 2012-08-03 14:07:26

+0

你實現'控制器:didChangeObject:atIndexPath: forChangeType:newIndexPath:'?這是針對單元的 - 您的實現是針對單元的。 – Daniel 2012-08-03 14:11:21

回答

0

幾天後,我決定了一個不同的邏輯來解決這個問題。我首先在fetchedResultsController上控制fetchedObjects的計數,如果計數大於1,我調用刪除&在數據庫上保存委託,但是如果計數等於1,只需推送到不同的viewController。在那個vc上,我會禁用導航控制器回到這個視圖控制器,直到有新的消息出現。而當有新郵件到來的時候,我會先刪除實體,不是增加消息的對象..

下面是一些僞代碼&算法:

 if ([[self.fetchedResultsController fetchedObjects]count] >1) { 
     callDeletionDelegateForDatabase; 
     }else{ 
     declare a viewController object; 
     pushToThatVC; 
     } 
//在類目標VC

hide_back_navigation_back_to_theViewControllerYouCameFrom; 
    //when a new message comes from web service 
    reset Entitiy(the property we need its value) 
    show_navigation_Controller_To_theViewControllerOfMessages... 

我發佈了這個愚蠢的編碼僞以幫助其他面臨同樣情況的人,如果有人知道更好的方法,請寫下來。 下面是我的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     if ([[self.fetchedResultsController fetchedObjects]count] >1) { 

     NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext]; 
      [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; 

     // Save the context. 
     NSError *error = nil; 
     if (![context save:&error]) { 
      /* 
      Replace this implementation with code to handle the error appropriately. 

      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. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
      } 
     }else { 
      BNT_DetailViewController *vc=[BNT_DetailViewController new]; 
      [self.navigationController pushViewController:vc animated:YES]; 

      XLog(@"You Cant delete this"); 
     } 
    } 

} 
相關問題