2011-12-12 35 views
1

我注意到插入數據在Core Data將使用tableView:cellForRowAtIndexPath:方法刷新我的Table View中的每個單元格。NSFetchedResultsController:不刷新每個新的單元格

我的應用程序首先從Web服務中獲取100多個項目。將它們添加到核心數據數據庫後,將爲每個單元撥打一個tableView:cellForRowAtIndexPath:呼叫。我期望只刷新第一個8-9可見單元格。我該如何改變它?這是一個問題的原因是我的應用程序在第一次調用tableView:cellForRowAtIndexPath時下載封面圖像 - 並且在每個單元調用它時,這不是最佳解決方案。

EDIT1: 我的代碼是完全基於來自CS 192p course at Stanford University一個例子。我已經從講座14下載了它們的Photomania.zip,並插入了tableView:willDisplayCell:forRowAtIndexPath:方法,該方法在調用方法時在日誌中顯示項目名稱。你可以在這裏獲取我的代碼:http://uploads.demaweb.dk/Photomania.zip。 正如你所看到的,它看起來像這個方法被稱爲表中的每個項目?爲什麼?

EDIT2: 我在iOS Developer Library的NSFetchedResultsControllerDelegate中使用了這種方法。我發現withRowAnimation:UITableViewRowAnimationFade導致它爲每個單元調用cellForRowAtIndexPathwillDisplayCell。將其更改爲withRowAnimation:UITableViewRowAnimationNone時,僅調用可見單元格。

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

回答

1

您是否正在重複使用您的單元格(即提供單元重用標識符)?

您是否正在實施NSFetchedResultsControllerDelegate方法,這將允許您的視圖控制器插入/刪除行時,他們來「聯機」,而不是做[tableView reloadData]?在這些方法中,您應該使用'[UITableView insertRowsAtIndexPaths:withRowAnimation:],[UITableView deleteRowsAtIndexPaths:withRowAnimations:]等來調整引用的行。

最後,您可以改爲將圖像加載邏輯移動到UITableView委託方法tableView:willDisplayCell:forRowAtIndexPath:。這種方式僅在實際即將顯示時才被調用。

+0

是的,我正在使用'NSFetchedResultsControllerDelegate'協議,它的實現幾乎像文檔:http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsControllerDelegate_Protocol/Reference/Reference。 HTML。 這是正常的,當所有的數據變得可用時,NSFetchedResultsControllerDelegate會產生一個'tableView:cellForRowAtIndexPath'? 感謝您關於'tableView:willDisplayCell:forRowAtIndexPath:'的通知。這可能會解決我的問題,但我仍然不同尋常的是,每個細胞都會調用細胞方法。 – dhrm

+0

'tableView:willDisplayCell:forRowAtIndexPath:'不起作用。請看我的EDIT1。 – dhrm

+0

使用'NSFetchedResultsController'時,只有兩種方式調用'tableView:cellForRowAtIndexPath:',當您在tableView上顯式調用'reloadData'時,或者當您插入/刪除行/部分以響應委託調用時。您需要通過調試器監視代碼中的那些區域,以查看導致完整表重新加載的原因。 「NSFetchedResultsCoordinator」絕不直接與表格視圖本身交互。 – gschandler

相關問題