gpt4 book ai didi

iphone - 核心数据: UITableView with multiple NSFetchedResultControllers

转载 作者:行者123 更新时间:2023-12-03 18:16:43 24 4
gpt4 key购买 nike

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

核心数据模板项目未演示如何执行此操作。所有内容(主要是索引路径)都是在不考虑节的情况下进行编码的(默认模板中没有节),并且所有内容都取自单个 NSFetchedResultController。是否有任何示例项目或文档可以演示如何执行此操作?

谢谢

最佳答案

假设您的 header 中包含以下内容(下面的代码可能有点草率,我很抱歉):

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

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

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

为其指定部分标题:

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

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

- (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;
}

关于iphone - 核心数据: UITableView with multiple NSFetchedResultControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2308487/

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