核心数据:带有多个 NSFetchedResultController 的 UITableView [英] Core Data: UITableView with multiple NSFetchedResultControllers

查看:19
本文介绍了核心数据:带有多个 NSFetchedResultController 的 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的很简单.在我的 UITableViewController 中,我想从多个 NSFetchedResultController 加载数据(我的数据模型中有多个实体),并将每个实体的数据放入表视图中的不同部分.因此,例如,从第一个 NSFetchedResultController 获取的所有项目将进入 UITableView 的第 0 部分,从另一个获取的项目进入第 1 部分,等等.

What I want to do is pretty simple. In my UITableViewController, I want to load data from multiple NSFetchedResultControllers (I have multiple entities in my data model) and put data from each one into a different section in the table view. So for example, all the fetched items from the first NSFetchedResultController would go in section 0 in the UITableView, the fetched items from the other one goes into section 1, etc.

Core Data 模板项目没有演示如何执行此操作.一切(主要是索引路径)都是在不考虑节的情况下编码的(默认模板中没有节),一切都来自单个 NSFetchedResultController.是否有任何示例项目或文档可以证明这样做?

The Core Data template project doesn't demonstrate how to do this. Everything (mainly the index paths) is coded without taking sections into account (there are no sections in the default template) and everything is taken from a single NSFetchedResultController. Are there any example projects or documentation that demonstrates doing this?

谢谢

推荐答案

在你的标题中假设以下内容(下面的代码会有点草率,我很抱歉):

Assume for a moment the following in your header (code below will be slightly sloppy, my apologies):

NSFetchedResultsController *fetchedResultsController1; // first section data
NSFetchedResultsController *fetchedResultsController2; // second section data

让表格知道您想要有 2 个部分:

Let the table know you want to have 2 sections:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2; // you wanted 2 sections
}

给它部分标题:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [NSArray arrayWithObjects:@"First section title", @"Second section title", nil];
}

让表格知道每个部分有多少行:

Let the table know how many rows there are per sections:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [[fetchedResultsController1 fetchedObjects] count];
    } else if (section == 1) {
        return [[fetchedResultsController2 fetchedObjects] count];
    }

    return 0;
}

构建单元:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ... // table cell dequeue or creation, boilerplate stuff

    // customize the cell
    if (indexPath.section == 0) {
        // get the managed object from fetchedResultsController1
        // customize the cell based on the data
    } else if (indexPath.section == 1) {
        // get the managed object from fetchedResultsController2
        // customize the cell based on the data
    }

    return cell;
}

这篇关于核心数据:带有多个 NSFetchedResultController 的 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆