gpt4 book ai didi

iPhone——使用 NSFetchedResultsController 将核心数据分解为多个部分

转载 作者:行者123 更新时间:2023-12-03 18:41:28 25 4
gpt4 key购买 nike

所以我已经成功实现了 Core Data 从服务器检索对象、保存它们并将它们显示在 UITableView 中。但现在,我希望将它们分成不同的部分。我已经找了几天了,NSFetchedResultsController 似乎让我感到困惑,尽管我使用它的方式有效。我的实体中有一个名为“articleSection”的键,当该项目与“Top”“Sports”“Life”等项目添加到核心数据时,会设置该键。我将如何在 UITableView 中将它们分成单独的部分?我读过有关使用多个 NSFetchedResultsController 的内容,但我对此感到非常沮丧。

任何建议或帮助将不胜感激。

最佳答案

documentation for NSFetchedResultsController有完美运行的示例代码。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = /* get the cell */;
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Configure the cell with data from the managed object.
return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [self.fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
<小时/>

设置获取请求的 sortDescriptors,以便结果按articleSection排序。
将sectionKeyPath 设置为“articleSection”,以便NSFetchedResultsController 为您创建这些部分。像这样的事情:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];;
request.fetchBatchSize = 20;
// sort by "articleSection"
NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"articleSection" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil];;

// create nsfrc with "articleSection" as sectionNameKeyPath
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"articleSection" cacheName:@"MyFRCCache"];
frc.delegate = self;
NSError *error = nil;
if (![frc performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
self.fetchedResultsController = frc;

关于iPhone——使用 NSFetchedResultsController 将核心数据分解为多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382116/

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