NSFetchedResultsController在每个字母中有单独的部分标题的AZ索引?

我有一个NSFetchedResultsController从核心数据结构,相册列表中提取数据。 它目前是由艺术家sorting,所以所有的A,B等我想添加一个索引,以便用户可以快速跳转到每个字母,我使用下面的代码来做到这一点。 问题在于节标题现在是“A”,“B”,“C”等,这意味着我已经按照字母顺序丢失了部分标题(“Adele”,“America “,”披头士乐队“等)

我希望索引使用字母A到Z,但节标题按字母顺序显示艺术家名称。 然后在索引中推送一封信,然后跳转到第一个带有该字母的艺术家。 我怎样才能做到这一点(在索引字母字符,但部分标题不同的属性)?

编辑:如果我将我的sectionNameKeyPath设置为我的author字段,AZ索引是可见的,但字母不同步到名称,例如,如果我点击S,它需要我以艺术家开始与AL,或攻G我到AB。 我不太确定这是为什么,也许该部分索引是搞砸了?

以下是我正在使用的代码:

在我的NSManagedObject子类中:

 - (NSString *) firstLetter { [self willAccessValueForKey: @"firstLetter"]; NSString *firstLetter = [[[self author] substringToIndex: 1] uppercaseString]; [self didAccessValueForKey: @"firstLetter"]; return firstLetter; } 

在我的视图控制器中,创buildFRC:

 NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest managedObjectContext: [[CDManager sharedManager] managedObjectContext] sectionNameKeyPath: @"firstLetter" cacheName: nil]; 

这里是我的sectionIndexTitlesForTableView:方法:

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

sectionNameKeyPath:@"author" 正确的参数,因为这是你希望你的表分组的部分。 标准实施

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

返回每个节头的第一个字母,所以不需要单独的firstLetter方法。

对于从索引到部分的正确同步,您必须实现表视图数据源方法tableView:sectionForSectionIndexTitle:atIndex: 与提取结果控制器相关的标准实现是:

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

编辑

迅速:

 func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { return fetchedResultsController.sectionIndexTitles } func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { return fetchedResultsController.sectionForSectionIndexTitle(title, atIndex: index) } 

Swift 2

将这个var添加到你的NSManagedObject

 class Adherent: NSManagedObject { var initialNom: String { self.willAccessValueForKey("initialNom") let initial = (self.nom as NSString).substringToIndex(1) self.didAccessValueForKey("initialNom") return initial } } 

然后在你的NSFetchedResultsController构造函数中使用它(不要忘记用“真实”属性进行sorting

 let nomSort = NSSortDescriptor(key: "nom", ascending: true) request.sortDescriptors = [nomSort] self.fetchedResultController = NSFetchedResultsController( fetchRequest: request, managedObjectContext: managedObjectContext, sectionNameKeyPath: "initialNom", cacheName: nil) 

在你的表格视图中 ,实现(至less)这些:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return self.fetchedResultController.sections!.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let sections = self.fetchedResultController.sections! let sectionInfo = sections[section] return sectionInfo.numberOfObjects } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { [...] } func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { return self.fetchedResultController.sectionIndexTitles } func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { return self.fetchedResultController.sectionForSectionIndexTitle(title, atIndex: index) } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return self.fetchedResultController.sectionIndexTitles[section] }