OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 应用程序上同步 UITableView 滚动和 CoraData 获取? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:27
标题: ios - 如何在 iOS 应用程序上同步 UITableView 滚动和 CoraData 获取?

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

我的应用程序中有核心数据库。在该数据库中,一个模型 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4