NSFetchedResultsController 由字符串的第一个字母创建的部分 [英] NSFetchedResultsController with sections created by first letter of a string

查看:16
本文介绍了NSFetchedResultsController 由字符串的第一个字母创建的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPhone 上学习核心数据.似乎很少有关于 Core Data 用部分填充表格视图的例子.CoreDataBooks 示例使用部分,但它们是从模型中的完整字符串生成.我想按照姓氏的第一个字母将 Core Data 表组织成多个部分,就像地址簿一样.

Learning Core Data on the iPhone. There seem to be few examples on Core Data populating a table view with sections. The CoreDataBooks example uses sections, but they're generated from full strings within the model. I want to organize the Core Data table into sections by the first letter of a last name, a la the Address Book.

我可以进入并为每个人创建另一个属性,即单个字母,以充当分区划分,但这似乎很笨拙.

I could go in and create another attribute, i.e. a single letter, for each person in order to act as the section division, but this seems kludgy.

这就是我要开始的......这个技巧似乎是在愚弄sectionNameKeyPath:

Here's what I'm starting with ... the trick seems to be fooling the sectionNameKeyPath:

- (NSFetchedResultsController *)fetchedResultsController {
//.........SOME STUFF DELETED
    // Edit the sort key as appropriate.
    NSSortDescriptor *orderDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:orderDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = 
            [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
            managedObjectContext:managedObjectContext 
            sectionNameKeyPath:@"personName" cacheName:@"Root"];
//....
}

推荐答案

我想我还有另一个选择,这个选择使用 NSString 上的类别...

I think I've got yet another option, this one uses a category on NSString...

@implementation NSString (FetchedGroupByString)
- (NSString *)stringGroupByFirstInitial {
    if (!self.length || self.length == 1)
        return self;
    return [self substringToIndex:1];
}
@end

现在稍后,在构建您的 FRC 时:

Now a little bit later on, while constructing your FRC:

- (NSFetchedResultsController *)newFRC {
    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:awesomeRequest
            managedObjectContext:coolManagedObjectContext
            sectionNameKeyPath:@"lastName.stringGroupByFirstInitial"
            cacheName:@"CoolCat"];
    return frc;
}

这是我现在最喜欢的方法.更清洁/更容易实施.此外,您不必对对象模型类进行任何更改来支持它.这意味着它可以在任何对象模型上工作,只要部分名称指向基于 NSString 的属性

This is now my favorite approach. Much cleaner/easier to implement. Moreover, you don't have to make any changes to your object model class to support it. This means that it'll work on any object model, provided the section name points to a property based on NSString

这篇关于NSFetchedResultsController 由字符串的第一个字母创建的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆