【发布时间】:2010-06-19 21:38:13
【问题描述】:
我的 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 示例。这是错误:
严重的应用程序错误。一个 从委托中捕获了异常 NSFetchedResultsController 在一个 调用 -controllerDidChangeContent:。 *** -[NSMutableArray removeObjectAtIndex:]: 超出索引 1 使用 userInfo (null) 限制 [0 .. 0]
之后,编辑模式(删除按钮出现的地方)不再起作用。
谢谢。
【问题讨论】:
标签: iphone uitableview nsfetchedresultscontroller