【发布时间】:2013-01-16 08:47:10
【问题描述】:
我有一个包含 1 节和 2 行的简单表格视图。我使用NSFetchedResultsController 来保持表格与CoreData 同步。我对 CD 中的一行进行了更改,这会触发要更新和移动的表格视图单元格。问题是当cellForRowAtIndexPath: 在NSFetchedResultsChangeUpdate 期间被调用时,会返回错误的单元格(这很有意义,因为单元格尚未移动)。因此,错误的单元格会使用新更新的数据进行更新。之后处理NSFetchedResultsChangeMove 消息,以便单元格交换位置(单元格的内容都不会更新,因为它只是一个移动调用)。结果是两个单元格都反映了来自新更新的 CD 实体的数据。重新加载表可以解决问题。我正在运行 iOS 6。
换句话说,如果索引 0 处的单元格代表实体 A,索引 1 代表实体 B,并且我将实体 A 更新为 A',以使 2 个单元格的顺序相反,结果是我看到 0:A' 1:A当我期望0:B,1:A'时。
- (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:
//the wrong cell is updated here
[self configureCell:(SyncCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
//this code produces errors too
//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
【问题讨论】:
标签: ios uitableview ios6 nsfetchedresultscontroller