ios - 如何使用 NSFetchedResultsController 更新 UITableView
问题描述
TableViewCell 未创建,而 numberOfRowsInSection 中的对象数量与形成的单元格总数相比更多
解决方案
您可以为获取的结果控制器设置委托,控制器注册以接收来自其托管对象上下文的更改通知。影响结果集或部分信息的上下文中的任何更改都将得到处理,并相应地更新结果。当结果对象更改位置或修改部分时,控制器会通知委托(请参阅 NSFetchedResultsControllerDelegate)。您通常使用这些方法来更新表视图的显示。
您可以使用 controllerWillChangeContent: 和 controllerDidChangeContent: 对表格视图的更新进行括号,其内容由获取的结果控制器提供,如图所示。
- (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:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
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: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)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
推荐阅读
- mongoose - 使用 VusJS 和 Axios,子组件不会通过数据绑定更新
- ios - 如何像标题一样将单元格固定到 collectionView 的顶部?
- mysql - 循环并使用存储的 MySql 进行选择
- square - 在正方形中获取属于客户的订单列表
- jsf - 使用 Primefaces 在同一列中创建具有两个过滤器的自定义过滤器
- mysql - sql presto 查询以可交互方式连接 2 个表
- docker - Docker 镜像 dotnet-framework 4.7.2-sdk 缺少 SDK 文件
- vba - 比较两个工作簿并删除匹配的行
- python - 是否有任何带有 key 参数的 set() Python 实现?
- azure-data-lake - uSQL 超出最大请求长度