我的 UITableViewController 或 NSFetchedResultsController 有一点问题。我不确定哪个是问题,但我猜它是 UITableViewController。
正如我所说,我使用 NSFetchedResultsController 将数据填充到 UITableViewController 中。数据按日期排序,也按日期-年份分段显示,例如 2010 年 5 月/2010 年 6 月/等。这显示为节标题。
现在,当我添加新数据时,它会自动使用当前日期作为默认日期,但如果我想使用当前不在部分列表中的日期,例如 2010 年 4 月(列表中当前为 5 月和 6 月),那么这是未正确显示。只有当我退出应用程序并重新启动它时,它才会显示新的部分标题,一切都很好。
所以我不知何故需要能够刷新我的列表或更新它。只是将它添加到上下文并更改它似乎并没有更新它。
同样,我不确定我需要更新什么,是我的 NSFetchedResultsController 对象还是 UITableViewController。
更新#1:
我只是认为这种方法有一个例外:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
}
这是我移动数据的地方:
- (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];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
此方法几乎取自 Apple 的 CoreDataBooks 示例。这是错误:
严重的应用程序错误。在调用 -controllerDidChangeContent: 期间,从 NSFetchedResultsController 的委托中捕获了一个异常。*** -[NSMutableArray removeObjectAtIndex:]: 索引 1 超出范围 [0 .. 0] 与 userInfo (null)
之后,编辑模式(出现删除按钮的位置)不再起作用。
谢谢。