2014-02-27 145 views
0

所以我有一個UITabelView使用自定義UITableViewCells。我追查這個問題,並在這個方法中「controllerDidChangeContent」,這個方法在表格加載時被調用了大約5次。由於核心數據更新而調用此方法。當tableview第一次加載UITableViewCells閃爍

我可以阻止發生閃爍,如果我註釋掉任何有<的行 - 指向它。我相信我們不想將[self reloadData]註釋掉,因爲在更改核心數據時會刷新數據。但我不確定endUpdates是否可以評論?

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [[self tableView] endUpdates]; <-- 
    [[self tableView] reloadData]; <-- 
    [UIView dismissIndicator]; 
} 

但是,如果我註釋掉endUpdates線,那麼所有的表中的數據沒有得到寫入的UITableView。有幾個單元格丟失。

我在想這個問題不是這個方法或它的內部方法,而是如何繪製單元格內容?

回答

3

您不需要在endUpdates之後撥打reloadData。您在beginUpdatesendUpdates區塊內對UITableView數據源進行了更改,因此您無需致電reloadData。如果您的整個數據源發生更改,則可撥打reloadData,因爲這可能會導致相對較高的費用。使用beginUpdatesendUpdates可以在不重新加載整個UITableView的情況下更改/插入/刪除數據源中的項目。

這是它應該看起來像(taken from Ray Wenderlich tutorial)什麼:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 
} 


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

    UITableView *tableView = self.tableView; 

    switch(type) { 

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

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

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

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


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id)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; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 

如果這沒有幫助您解決問題,那麼我需要看到更多的代碼,具體在哪裏,你的beginUpdates並在執行數據源更新endUpdates塊。

+0

但是,當我添加一個新的單元格時,將reloadData從controllerDidChangeContent中取出時,它會添加一個額外的空白單元格。 – jdog

+0

還有一些導致閃爍的問題,但是這種代碼是正確的方法。 – jdog

+0

嘿jdog,什麼是導致閃爍的問題。你能告訴我嗎? – Sourabh

0

爲什麼不直接調用reloadSections方法而不是[self.tableView reloadData];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];