• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 iOS 应用程序上同步 UITableView 滚动和 CoraData 获取?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:27:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想实现下面的功能,你能帮帮我吗

我的应用程序中有核心数据库。在该数据库中,一个模型 CourseEvents 包含超过 150000 条记录,每条记录有大约 12 个字段。

每个记录一个 UITableViewCell 的值。

但我不想在单个获取请求中获取所有记录。想要根据 UITableView 滚动获取一些 N 条记录。

例子:

当 TableView 第一次加载时想要获取 200 条记录,每当用户滚动 UITableView 时需要获取下一个 200 条记录,就像需要根据 的滚动从模型中获取数据>UITableview.

我怎样才能做到这一点。请帮助.....



Best Answer-推荐答案


如果我正确理解您的问题,当您最初加载 View 时,您只想获取 200 条记录,而在 tableView Scroll 上,您想要获取下一个 200 条记录,依此类推。您正在使用核心数据,然后在 NSFetchedResultsController 的帮助下更容易获取记录.只需将 setFetchBatchSize 设置为您想要获取的任何记录(20 也应该适合您的情况)。网上有很多例子,也有很棒的苹果 sample 。这是CoreDataBooks的链接例子。这太棒了tutorial关于如何使用 NSFetchedResultsController。最后 Core Data Programming guide有没有你的帮助。这是关于如何使用 NSFetchedResultController 的示例代码

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }


}

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
        entityForName"FailedBankInfo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
        initWithKey"details.closeDate" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext sectionNameKeyPath:nil
            cacheName"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

- (NSInteger)tableViewUITableView *)tableView
    numberOfRowsInSectionNSInteger)section {
    id  sectionInfo =
        [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (void)configureCellUITableViewCell *)cell atIndexPathNSIndexPath *)indexPath {
    EntityName *entity = [_fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = entity.name; //just example
    cell.detailTextLabel.text = [NSString stringWithFormat"%@, %@",
        entity.city, entity.state];
}

- (UITableViewCell *)tableViewUITableView *)tableView
    cellForRowAtIndexPathNSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell =
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Set up the cell...
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

关于ios - 如何在 iOS 应用程序上同步 UITableView 滚动和 CoraData 获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23054560/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap