从核心数据中删除虚拟行的核心数据异常
我有一个与核心数据绑定的UITableView
。在没有条目的情况下,我修改表格以确保#sections = 1,#rows = 1,并且该行显示“您的表格为空;使用+按钮添加新项目”的文本。从核心数据中删除虚拟行的核心数据异常
但是,当用户通过单击+按钮添加第一个项目时,核心数据将抛出异常,表示更新后的行数(1)与更新前的行数不等(1)加号或减号(1插入,0删除)。这是有道理的,因为核心数据可能会查询我的tableview所做的相同功能(numberOfSectionsInTableView
和numberOfRowsInSection:section
)。但是,如何告诉核心数据我在撒谎到tableview,并且更新前的行数实际上是0?
好的,我认为我想到一个非常冒险的方式来处理这个问题,因为我确实保留了一个UITableView
。记下它以防将来有用。
基本上,创建一个BOOL isUpdating
来记录我们目前是否正在更新表中。执行numberOfRowsInSection:section
应检查self.isUpdating
是否为真;如果是,则返回“lie”行数(即NSFetchedResultsController
期望该表具有的数字),否则返回行数以告知tableview。然后在controllerWillChangeContent
中将isUpdating设置为true,并在controllerDidChangeContent
中设置为false。
这将有助于显示如何添加行,维护状态等代码。否则,任何答案只会是非常普遍的或猜测。
你能赶上在NSFetchedResultsControllerDelegate
方法异常喜欢这个样本
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
if ([[self.fetchedResultsController sections] count] != 1) {
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeDelete:
if ([[self.fetchedResultsController sections] count] != 0) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
中如果你没有在你的桌子抓住它的部分在controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
方法来代替。