Custom Core Data SectionNameKeyPath

前端 未结 2 982
执笔经年
执笔经年 2020-12-14 09:20

I am new at core data and am trying to figure out how to create a custom sectionNameKeyPath in my NSFetchedResultsController. I have a managed obje

2条回答
  •  一向
    一向 (楼主)
    2020-12-14 09:45

    I recommend against using a transient property as the sectionNameKeyPath as it will result in faulting all objects obtained by the fetch request just so the property could be used as the section path.
    You better define a persistent property of year and use it as your sectionNameKeyPath.
    set you FRC sectionNameKeyPath to year like so:

    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:self.managedObjectContext
                                          sectionNameKeyPath:@"year"
                                                   cacheName:nil/*your chioce*/];
    

    to display the section name as a title in the table, implement:

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

提交回复
热议问题