来自NSFetchedResultsController的A-Z索引,每个字母内都有单独的节头?

时间:2013-04-11 08:25:37

标签: ios objective-c core-data indexing nsfetchedresultscontroller

我有一个NSFetchedResultsController从核心数据结构中获取数据,这是一个专辑列表。它目前按艺术家排序,所以所有的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;
}

在我的View Controller中,创建FRC:

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

这是我的sectionIndexTitlesForTableView:方法:

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

2 个答案:

答案 0 :(得分:18)

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)
} 

答案 1 :(得分:4)

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”属性排序

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

self.fetchedResultController = NSFetchedResultsController(
    fetchRequest: request, 
    managedObjectContext: managedObjectContext, 
    sectionNameKeyPath: "initialNom", 
    cacheName: nil)

表格视图中,实施(至少)以下内容:

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]
}