使用 NSFetchedResultsControllerDelegate 更新 TableView 部分和NSPredicate 更改后的行

发布于 2024-10-09 22:18:17 字数 366 浏览 1 评论 0原文

有没有办法在更改 fetchedResultsControllers NSpredicate (并执行提取)后更新 UITableView 部分和行?当我的 searchBar 中的 searchText 更改时,我想使用controllerWillChangeContent 委托方法来更新我的表。

目前在我的应用程序中,仅当核心数据发生更改时才会调用这些委托方法。更改 resultsController 谓词时不适用执行获取。

如果我只是在每次文本更改时调用 [self.tableView reloadData],则不会有动画,并且会有点阻碍体验。

或者我应该做一些完全不同的事情来更新每个搜索文本条目更改的 tableView 内容?

Is there a way to update a UITableView sections and rows after changing the fetchedResultsControllers NSpredicate (and executing the fetch)? I would like to use controllerWillChangeContent delegate methods to update my table when the searchText in my searchBar is changed.

Currently in my app, these delegate methods only get called when changes in coredata occur. Not when changing the resultsController predicate & executing the fetch.

If I simply call [self.tableView reloadData] on each textchange, there are no animations and it blocks the experience a bit.

Or should I do something completely different to update the tableView contents on each search text entry change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久隐师 2024-10-16 22:18:17

我再次查看了 Apple 的文档,看起来当谓词更改时不会调用 NSFetchedResultsControllerDelegate 方法:

一个实例
NSFetchedResultsController 使用
本协议中的方法通知其
委托控制器的获取
结果已更改,因为
添加、删除、移动或更新
操作。

因此,回答我自己的问题:在 NSFetchedResultsController 谓词更改后,不可能使用 NSFetchedResultsControllerDelegate 来更新表。

I've had another look into Apple's documentation and it looks like the NSFetchedResultsControllerDelegate methods are not called when a predicate has changed:

An instance of
NSFetchedResultsController uses
methods in this protocol to notify its
delegate that the controller’s fetch
results have been changed due to an
add, remove, move, or update
operations.

So, to answer my own question: It's not possible to use NSFetchedResultsControllerDelegate to update the table after NSFetchedResultsController predicate changes.

看春风乍起 2024-10-16 22:18:17

尝试调用 [self.tableView reloadData]

-(void)viewWillAppear:(BOOL)animated;
                 or
-(void)viewDidAppear:(BOOL)animated;

Try to call [self.tableView reloadData] in

-(void)viewWillAppear:(BOOL)animated;
                 or
-(void)viewDidAppear:(BOOL)animated;
七禾 2024-10-16 22:18:17

您可以尝试使用 UITableView 方法 reloadSections:withRowAnimation: 传入包含所有部分的 NSIndexSet 。这样您应该重新加载整个内容,但仍然可以获得您选择的动画。

如果要添加和删除行,则需要使用 insertSections:withRowAnimation:removeSections:withRowAnimation:。如果您要同时进行多项更改,则还应该在调用 beginUpdatesendUpdates 之间进行这些更改。

You could try using the UITableView method reloadSections:withRowAnimation: passing in an NSIndexSet with all of your sections. That way you should have the whole thing reload but still get the animation of your choice.

If you are adding and removing rows you will need to use insertSections:withRowAnimation: and removeSections:withRowAnimation:. If you're doing a number of changes at once you should also do them between calls to beginUpdates and endUpdates.

子栖 2024-10-16 22:18:17

来自 NSFetchedResultsController 类参考:

修改获取请求

您不能简单地将获取请求更改为
修改结果。如果你想
更改获取请求,您必须:

如果您正在使用缓存,请将其删除
(使用deleteCacheWithName:)。
通常你不应该使用缓存
如果您要更改获取请求。

更改获取请求。调用
执行Fetch:.

由于您已经更改了谓词,因此必须再次调用performFetch:,然后委托将处理从那里更新表的操作。

From NSFetchedResultsController Class Reference:

Modifying the Fetch Request

You cannot simply change the fetch request to
modify the results. If you want to
change the fetch request, you must:

If you are using a cache, delete it
(using deleteCacheWithName:).
Typically you should not use a cache
if you are changing the fetch request.

Change the fetch request. Invoke
performFetch:.

Since you have changed the predicate, you must call performFetch: once again and then the delegate will handle updating the table from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文