我试图创建一个聚合提要的应用程序。
当表格视图结束显示单元格时,我想更改馈送状态。
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
Feed* feed = [_fetchedResultsController objectAtIndexPath:indexPath];
if ([feed.read boolValue] == YES) return ;
feed.read = [NSNumber numberWithBool:YES];
[Feed save];
}
我使用NSFetchedResultsController,当我更改提要状态时,我使用reloadRowsAtIndexPath。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
...
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
...
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
我滚动,didEndDisplayingCell
用好的indexPath
调用,当tableView
结束更新时,它用下一个indexPath
再次调用didEndDisplayingCell
。在那之后,我从核心数据中得到了一个严重的错误。
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. *** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2] with userInfo (null)
有什么想法吗?
为什么要在didEndDisplayingCell
中更新coreData
?通过调用[Feed save]
,则tableview
将再次开始更新。tableView
将调用didEndDisplayingCell
,因为您重新加载了刚刚更新的行。
所以把代码放在didEndDisplayingCell
方法的其他地方。