UITableView NSFetchedResultsController 哲学
我使用 NSFetchedResultsController
作为 NSManagedObjects
的容器在 UITableView
中显示来自 CoreData
的数据。删除行的代码运行良好,但我的实现让我觉得不太优雅。有更好的办法吗?
下面的代码说明了删除事件:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[myContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
if (![myContext save:&error]) {
// Error handling
}
fetchedResultsController = nil;
if (![[self getFetchedResultController] performFetch:&error]) {
// Error handling
}
[tableView reloadData];
}
}
我将 fetchedResultsController
设置为 nil
并执行另一次提取以更新新内容。
让我担心的是,我在各种论坛上看到了一些参考资料,暗示上下文的更改应该会自动引起 fetchedResultsController 的更改,也许第二次获取是不必要的。目前我还没有实现部分。
另一个问题是代码只是让行消失而没有任何动画。
可以介绍一些流畅的动画吗?
I am displaying data from CoreData
in a UITableView
using NSFetchedResultsController
as the container for the NSManagedObjects
. The code for deleting a row works well, but my implementation strikes me as less than elegant. Is there a better way?
The code below illustrates the deletion event:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[myContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
if (![myContext save:&error]) {
// Error handling
}
fetchedResultsController = nil;
if (![[self getFetchedResultController] performFetch:&error]) {
// Error handling
}
[tableView reloadData];
}
}
I set the fetchedResultsController
to nil
and perform another fetch to update the new contents.
What concerns me is that I have seen references on various forums hinting that a change in the context should automatically engender changes in the fetchedResultsController and perhaps this second fetch is unnecessary. I have not implemented sections at this point.
Another concern is that the code simply makes the row disappear without any animation.
Could some slick animation be introduced?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
研究使用 NSFetchedResultsControllerDelegate 协议方法 管理对表和数据库的更改。这样,每当您在 NSFetchedResultsController 的 NSManagedObjectContext 中进行保存时,托管对象上下文都会通知 fetchedResultsController。然后 fetchedResultsCotnroller 将调用其委托方法。
您想要实现controllerWillChangeContent: 和controllerDidChangeContent:,这样您就可以将其他委托方法的调用与对表视图的beginUpdates 和endUpdates 方法的调用括起来。 (请参阅参考资料中的示例。)这可以防止表视图与 fetchedResultsController 不同步。
然后删除这些行:
完成此操作后,您可以在插入和删除中引入动画细胞。
Look into using the NSFetchedResultsControllerDelegate Protocol methods to manage changes to the table and the database. This way, whenever you do a save within the NSManagedObjectContext of the NSFetchedResultsController, that fetchedResultsController will be notified by the managed ojbect context. Then the fetchedResultsCotnroller will call its delegate methods.
You want to implement controllerWillChangeContent: and controllerDidChangeContent: so you can bracket the calls of the other delegate methods with calls to the beginUpdates and endUpdates methods for the table view. (See the example in the reference.) This keeps the table view from getting out of synchronization with the fetchedResultsController.
Then delete these line:
Once you have done this, you can introduce animations in the insertion and deletion of cells.