来自NSFetchedResultsController的AZ索引,每个字母中都有单独的节标题?

Luk*_*uke 10 indexing core-data objective-c nsfetchedresultscontroller ios

我有一个NSFetchedResultsController从核心数据结构,一个专辑列表中获取数据.它目前按艺术家排序,所以所有的A,B等等.我想添加一个索引,以便用户可以快速跳转到每个字母,我正在使用下面的代码来完成它.问题是节标题现在也是"A","B","C"等,这意味着我已经按字母顺序丢失了节目标题,每个艺术家都在其上("Adele","America" ","甲壳虫乐队,"等等)

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

编辑:如果我把我的sectionNameKeyPath给我的author领域,AZ指数是可见的,但如果我点击S上的字母不同步到的名称,例如,它需要我的艺术家与AL开头,或攻摹带我去AB.我不太清楚为什么会这样,也许这部分索引搞砸了?

这是我正在使用的代码:

在我的NSManagedObject子类中:

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

在我的View Controller中,要创建FRC:

NSFetchedResultsController *byAuthorFRC = [[NSFetchedResultsController alloc] initWithFetchRequest: _fetchRequest
                                                                              managedObjectContext: [[CDManager sharedManager] managedObjectContext]
                                                                                sectionNameKeyPath: @"firstLetter"
                                                                                         cacheName: nil];
Run Code Online (Sandbox Code Playgroud)

这是我的sectionIndexTitlesForTableView:方法:

- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
    return [self.fetchedResultsController sectionIndexTitles];
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 18

sectionNameKeyPath:@"author" 正确的参数,因为这是您希望将表分组为多个部分的方式.标准实施

- (NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
    return [self.fetchedResultsController sectionIndexTitles];
}
Run Code Online (Sandbox Code Playgroud)

返回每个节头的第一个字母,因此不需要单独的 firstLetter方法.

要从段索引到章节的正确同步,您必须实现表视图数据源方法tableView:sectionForSectionIndexTitle:atIndex:.与获取的结果控制器相关的标准实现是:

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

编辑:

迅速:

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)
} 
Run Code Online (Sandbox Code Playgroud)


Ale*_* G. 6

斯威夫特 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
    }

}
Run Code Online (Sandbox Code Playgroud)

然后在您的NSFetchedResultsController构造函数中使用它(不要忘记使用“real”属性进行排序

let nomSort = NSSortDescriptor(key: "nom", ascending: true)
request.sortDescriptors = [nomSort]

self.fetchedResultController = NSFetchedResultsController(
    fetchRequest: request, 
    managedObjectContext: managedObjectContext, 
    sectionNameKeyPath: "initialNom", 
    cacheName: nil)
Run Code Online (Sandbox Code Playgroud)

在您的表视图中,实现(至少)这些:

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]
}
Run Code Online (Sandbox Code Playgroud)