我目前正在进行一个使用UITableView和NSFetchedResultsController存储数据的项目。我面临的问题是区段的使用不起作用。
我希望显示为表的实体具有以下特征:
我希望有基于服务器的部分,每个部分中的行就是通道。布局如下:
但是,问题是,当我试图在NSFetchedResultsController中插入/删除/更改元素时,应用程序将得到一个异常,如下所示:
CoreData:错误:严重的应用程序错误。在调用NSFetchedResultsController :时,从-controllerDidChangeContent的委托中捕获了一个异常。无效更新:无效的节数。更新(3)后表视图中包含的节数必须等于更新(2)之前表视图中包含的节数,加上或减去插入或删除的节数(0插入,0删除)。使用userInfo (null)
我做了一些研究,人们说这是因为UITableViewController代表首先触发,并试图显示当时不存在的元素/区段。我还没有找到解决办法,这就是我来这里的原因。
这是我的NSFetchedResultsController委托方法
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
NSLog(@"--- Core Data noticed change");
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}非常感谢
罗伯特
发布于 2012-11-08 18:41:42
您还必须实现
controller:didChangeSection:atIndex:forChangeType:函数,否则将不会通知表视图在核心数据存储区中插入或删除的部分。
您可以在NSFetchedResultsControllerDelegate协议参考中找到一个示例实现。
https://stackoverflow.com/questions/13294732
复制相似问题