Ios NSFetchedResultsController中的A-Z索引,每个字母中有单独的节标题?

Ios NSFetchedResultsController中的A-Z索引,每个字母中有单独的节标题?,ios,objective-c,core-data,indexing,nsfetchedresultscontroller,Ios,Objective C,Core Data,Indexing,Nsfetchedresultscontroller,我有一个NSFetchedResultsController,它从核心数据结构(一个相册列表)中获取数据。它目前是按艺术家排序的,所以所有的A、B等。我想添加一个索引,以便用户可以快速跳转到每个字母,我使用下面的代码来完成。问题是,现在的章节标题也是“A”、“B”、“C”等,这意味着我已经按照每个艺术家的字母顺序丢失了章节标题(“阿黛尔”、“美国”、“披头士”等) 我希望索引使用字母A到Z,但节标题以字母顺序显示艺术家姓名。在索引中输入一个字母会跳转到第一个有该字母的艺术家。我如何实现这一点(索

我有一个
NSFetchedResultsController
,它从核心数据结构(一个相册列表)中获取数据。它目前是按艺术家排序的,所以所有的A、B等。我想添加一个索引,以便用户可以快速跳转到每个字母,我使用下面的代码来完成。问题是,现在的章节标题也是“A”、“B”、“C”等,这意味着我已经按照每个艺术家的字母顺序丢失了章节标题(“阿黛尔”、“美国”、“披头士”等)

我希望索引使用字母A到Z,但节标题以字母顺序显示艺术家姓名。在索引中输入一个字母会跳转到第一个有该字母的艺术家。我如何实现这一点(索引中的字母字符,但章节标题的属性不同)

编辑:如果我将我的
sectionNameKeyPath
设置为我的
作者
字段,A-Z索引是可见的,但字母不与名称同步,例如,如果我点击S,它会将我带到以AL开头的艺术家,或者点击G会将我带到AB。我不太清楚为什么会出现这种情况,可能是因为分区索引出错了

以下是我使用的代码:

在my
NSManagedObject
子类中:

- (NSString *) firstLetter
{
    [self willAccessValueForKey: @"firstLetter"];
    NSString *firstLetter = [[[self author] substringToIndex: 1] uppercaseString];
    [self didAccessValueForKey: @"firstLetter"];
    return firstLetter;
}
NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest
                                                                              managedObjectContext: [[CDManager sharedManager] managedObjectContext]
                                                                                sectionNameKeyPath: @"firstLetter"
                                                                                         cacheName: nil];
- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
    return [self.fetchedResultsController sectionIndexTitles];
}
在我的视图控制器中,要创建FRC:

- (NSString *) firstLetter
{
    [self willAccessValueForKey: @"firstLetter"];
    NSString *firstLetter = [[[self author] substringToIndex: 1] uppercaseString];
    [self didAccessValueForKey: @"firstLetter"];
    return firstLetter;
}
NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest
                                                                              managedObjectContext: [[CDManager sharedManager] managedObjectContext]
                                                                                sectionNameKeyPath: @"firstLetter"
                                                                                         cacheName: nil];
- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
    return [self.fetchedResultsController sectionIndexTitles];
}
这是我的
sectionIndexTitlesForTableView:
方法:

- (NSString *) firstLetter
{
    [self willAccessValueForKey: @"firstLetter"];
    NSString *firstLetter = [[[self author] substringToIndex: 1] uppercaseString];
    [self didAccessValueForKey: @"firstLetter"];
    return firstLetter;
}
NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest
                                                                              managedObjectContext: [[CDManager sharedManager] managedObjectContext]
                                                                                sectionNameKeyPath: @"firstLetter"
                                                                                         cacheName: nil];
- (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
构造函数中使用它(不要忘记使用“real”属性对排序)

在您的表视图中,实施(至少)以下各项:


如果使用
sectionNameKeyPath:@“author”
?@MartinR有一个A-Z索引,但字母与名称不同步,例如,如果我点击S,它会将我带到以AL开头的艺术家,或者点击G会将我带到AB。试试这个@DanShelly,我不确定我是否跟随。。在这个链接中没有提到索引。你建议我做什么?尝试实现:
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section