2012-07-26 68 views
1

後,這是關於我與NSFetchedResultsChangeInsert asked before here刷新表NSFetchedResultsChangeInsert

問題下一個問題下面的代碼是什麼我在NSFetchedResultsController委託使用。更新和刪除工作得很好,除了當我嘗試添加一個新的對象到我的模型。

case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [self configureCell:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:1]] atIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:1]];    
      break; 

我的問題是:如何在插入後更新tableview?當我點擊保存時,新的對象被創建。但是該表不會相應更新。我已經與

[self.tableView reloadData]; 

試圖到處我也試着到viewWillAppear中和ViewDidAppear內調用perfomFetch:

NSError *error = nil; 
if (![_fetchedResultsController performFetch:&error]) { 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 

但表將不會更新,直到我重新啓動應用程序。我花了4個小時尋找答案,但我陷入困境。任何幫助將不勝感激,謝謝

回答

2

應該沒有必要調用configureCell:插入事件,[tableView insertRowsAtIndexPaths:...]就足夠了。表數據源方法cellForRowAtIndexPath:將被調用來獲取新的單元格。

另外,indexPath == nil用於插入事件,因此您的configureCell:呼叫無法工作。

重要的一點是要撥打電話[tableView beginUpdates]controllerWillChangeContent:[tableView endUpdates]controllerDidChangeContent:

+0

thnaks你的方法。我按照你的指導原則刪除了configureCell:在NSFetchedResultsController委託上。然而,這對新插入似乎沒有多大幫助。它仍然是一樣的。它看起來像fetchRequest獲取所有內容,但TableView無法添加新行。 – sam80 2012-07-26 20:52:30

+0

您是否將對象添加到FRC使用的同一個受管對象上下文中?或者在不同的環境中 - 在這種情況下,您需要將更改合併到主環境中。其他操作(更改,刪除)是否正常工作? – 2012-07-26 21:12:42

+0

是的,除插入物外,一切都很完美。並且,是的,我正在使用相同的上下文,我幾乎要放棄:( – sam80 2012-07-26 21:19:01