【问题标题】:iPhone -- breaking up Core Data into sections with NSFetchResultsControlleriPhone——使用 NSFetchResultsController 将核心数据分解为多个部分
【发布时间】:2012-01-13 00:06:39
【问题描述】:

所以我已经成功地实现了 Core Data 来从服务器检索对象、保存它们并在 UITableView 中显示它们。但是现在,我希望将它们分成单独的部分。我已经找了几天了,NSFetchedResultsController 似乎让我很困惑,即使我使用它的方式是有效的。我的实体中有一个名为“articleSection”的键,该键是在将项目添加到 Core Data 时设置的,其中包含“Top”“Sports”“Life”等项目。我将如何在我的 UITableView 中将它们分成单独的部分?我已经阅读了有关使用多个 NSFetchedResultsControllers 的信息,但我对此感到非常沮丧。

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

【问题讨论】:

标签: iphone uitableview core-data nsfetchedresultscontroller


【解决方案1】:

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;

【讨论】:

  • 不想使用 NSFetchedResultsController 怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多