【发布时间】:2011-03-29 22:45:34
【问题描述】:
好的,伙计们,这就是我的情况。
我正在使用带有 Core Data 的 UITableViewController。该表有大约 200 个单元格,基本上用作清单。当您点击一个单元格时,该单元格有一个复选框,该复选框被设置为 UITableViewCell.imageView(分配给标签左侧的 UIImageView)。
每当我使用以下两种方法中的任何一种来更新表格时,更新大约需要 1 - 2 秒......据我所知,它似乎正在重新加载每个单元格。如何仅强制更新刚刚被点击的单元格?
[self.tableView reloadData]; 或
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (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:NO];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:NO];
break;
}
}
- (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:NO];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:NO];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:NO];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:NO];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
【问题讨论】:
-
如何更新模型中的数据?听起来所有单元格都已更新。
-
-insertSections:withRowAnimation:、-deleteSections:withRowAnimation:、-insertRowsAtIndexPaths:withRowAnimation:、-deleteRowsAtIndexPaths:withRowAnimation:的第二个参数不是BOOL;他们是UITableViewRowAnimation类型
标签: objective-c uitableview ios