如何刷新 UITableViewController 或 NSFetchedResultsController?
我的 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 超出 边界 [0 .. 0] 与 userInfo (null)
之后,编辑模式(出现删除按钮的地方)不再起作用。
谢谢。
I have a little problem with my UITableViewController or NSFetchedResultsController. I am not sure which is the problem soure but I guess its the UITableViewController.
As I said I use a NSFetchedResultsController to fill my data into a UITableViewController. The data is sorted by date and is also displayed in sections by date-year, e.g. May 2010/June 2010/ and so on. This is displayed as the section header.
Now when I add new data, it automatically uses the current date as default, but if I want to use a date, that is currently not in the section list, e.g. April 2010 (currently May and June in the list), then this is not displayed correctly. Only when I quit the app and start it again it will show the new section headers and everything is fine.
So I somehow need to be able to refresh my list or update it. By just adding it to the context and changing it does not seem to update it.
Again, I am not sure what I need to update, if it is my NSFetchedResultsController object or the UITableViewController.
UPDATE #1:
I just figured that there is an exception in this method :
- (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];
}
This is where I move the data :
- (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;
}
}
This method is pretty much taken from the CoreDataBooks example from Apple. And this is the error :
Serious application error. An
exception was caught from the delegate
of NSFetchedResultsController during a
call to -controllerDidChangeContent:.
*** -[NSMutableArray removeObjectAtIndex:]: index 1 beyond
bounds [0 .. 0] with userInfo (null)
And after that, the edit mode (where the delete button appears) does not work anymore.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要实现
controller:didChangeSection:atIndex:forChangeType:
委托方法。在其中,根据报告的更改向表视图发送insertSections:withRowAnimation:
、deleteSections:withRowAnimation:
和reloadSections:withRowAnimation:
消息类型。You need to implement the
controller:didChangeSection:atIndex:forChangeType:
delegate methods. In it, sendinsertSections:withRowAnimation:
,deleteSections:withRowAnimation:
, andreloadSections:withRowAnimation:
messages to your table view depending on the reported change type.好吧,看起来我找到了解决方案,我只是跳过这些
并始终这样做。
显然,我会给出更多逻辑,因为只有当我想将数据移动到以前不存在的部分时,才会出现问题。在有人能给我一个真正的解决方案之前,我会选择这个。
Ok, looks like I found a solution, I just skip these
and always do this.
I will obviously give this some more logic, since the problem only occures when I want to move my data in a section that does not exist before. Until someone can give me a real solution, I will go with this one.
采用 Apple 在 iPhoneCoreDataRecipes 版本 1.2 中的修复,正确的做法是替换
为
(FileMerge 是你的朋友!)
注意,版本 1.2 中还有另一个修复,可以防止创建新条目时出现不稳定的崩溃。我已报告此问题并在两周内修复。我鼓励您向 Apple 提交错误报告。他们的响应速度比您想象的要快得多,您可以期望获得最佳的解决方案。
Adopting the fix by Apple in iPhoneCoreDataRecipes in version 1.2, the proper thing to do is to replace
with
(FileMerge is your friend!)
Note, there's another fix in version 1.2 that prevents erratic crashing when creating new entries. I had reported this and it was fixed within two weeks. I encourage you to file bug reports with Apple. They're far more responsive than you think, and you can expect to get the best possible fix.