我目前正在开发一个项目,我使用UITableView和NSFetchedResultsController来存储我的数据 . 我面临的问题是使用部分不起作用 .
我想要显示为表的实体具有以下字符:
-
Channels (字符串)
-
服务器(字符串)
-
名称(字符串)
我希望有基于服务器的部分,每个部分中的行是通道 . 布局如下所示:
-
Server1
通道1
通道2 -
Server2
通道3
通道4
等等..
但问题是,当我尝试在NSFetchedResultsController中插入/删除/更改元素时,应用程序会获得如下异常:
CoreData:错误:严重的应用程序错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . 无效更新:无效的节数 . 更新后的表视图中包含的节数(3)必须等于更新前的表视图中包含的节数(2),加上或减去插入或删除的节数(插入0,0删除) . 用户信息(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];
}
非常感谢
罗伯特
1 回答
你还必须实现
委托函数,否则不会通知表视图核心数据存储中插入或删除的部分 .
您可以在NSFetchedResultsControllerDelegate Protocol Reference中找到示例实现 .