2012-07-16 119 views
0

我有一個UITableView顯示來自核心數據(人)的數據。當滾動瀏覽表格時,我從網上獲取(異步)該用戶的個人資料圖像,並在完成後更新核心數據對象以及單元格的圖像視圖。UITableViewCell圖像+保存到核心數據

但是,我遇到了一個問題,因爲我每次將圖像保存到核心數據(最終在用戶向下滾動時會減慢應用程序速度)會觸發- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {。在controllerDidChangeContent:我在表視圖上調用reloadData這當然是幀速率急劇下降的原因。

有關如何處理將圖像保存到核心數據對象並將其更新到適當位置的建議?

謝謝。

+0

爲什麼你需要'構建併發用戶界面?如果你正在做你懶加載正確,你不應該調用reloadData。據我瞭解,你插入到核心數據,所以該表不需要更新,因爲該表是首先爲核心數據提供圖像的。 – jacerate 2012-07-16 15:28:53

+0

數據可能在其他地方更改,所以如果數據發生更改,我想適當地更新表格。 – runmad 2012-07-16 15:36:40

回答

2

您可以響應NSFetchedResultsController中的更細微更改,這可能有所幫助(請參閱示例代碼)。

您可能還想看看今年的WWDC會議(2012年),討論如何改進UITableView中的滾動,特別是嘗試將處理量限制爲屏幕上的那些行的技術:會話211 - (空)controllerDidChangeContent` - 在iOS

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView beginUpdates]; 
} 

- (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]; 
       break; 

      case NSFetchedResultsChangeUpdate: 
       [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
       break; 

      case NSFetchedResultsChangeMove: 
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
       break; 
     } 
} 

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 
} 
+0

啊完美,我沒有意識到'reloadRowsAtIndexPaths:'方法,每次我點擊'NSFetchedResultsChangeUpdate'情況下重新加載單元格。謝謝! – runmad 2012-07-16 15:54:48