gpt4 book ai didi

ios - IOS 中使用 FetchLimit 和 Core Data 无限滚动 TableView

转载 作者:行者123 更新时间:2023-11-28 23:38:23 24 4
gpt4 key购买 nike

我在核心数据中有数千行数据。进行如此大的提取会显着降低用户体验。由于我让用户以各种方式查看数据,因此重新加载很常见,并且重新加载 FRC 可能会很痛苦。因此,我在 Fetch 中设置了 FetchLimit,例如 50。50 的速度非常快,但只提取一小部分行,因此根本无法访问较旧的数据。

我想实现一个无限滚动,以便当用户点击 TableView 的底部时加载新的行。我可以使用多种委托(delegate)方法中的任何一种来触发重新加载,例如:

  - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
// This is the last cell
[self getMore];
}
}

我的问题是使用什么获取更多。以下方法可以工作,但看起来很笨拙,因为您需要重新加载所有较早的单元格,并且很难跟踪您在 fetchLimit 中的位置。有没有更有效的方法来实现无限滚动?

   - (void)getMore {
[self.fetchedResultsController.fetchRequest setFetchLimit:newFetchLimit];
self.fetchedResultsController = nil;
NSError *error;
if (![self.fetchedResultsController performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

[self.tableView reloadData];
}

最佳答案

在 getMore 函数中,您可以使用 fetchOffset属性与 fetchLimit 相结合,因此您可以创建任意结果集的子范围,并避免再次重新加载所有单元格内容。

[fetchedResultsController.fetchRequest setFetchOffset: newFetchOffset];

newFetchOffset 将从 0 开始,在您的第二个请求中,您需要将其设置为 50,以便您可以获得接下来的第 50 个值。

关于ios - IOS 中使用 FetchLimit 和 Core Data 无限滚动 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com