我将核心数据用于表视图,并且我希望使用每个结果的第一个字母作为节头(这样我就可以在侧面获得节索引).有没有办法用密钥路径实现这一点?比如下面,我用name.firstLetter作为sectionNameKeyPath(不幸的是,这不起作用).

我必须手动抓取每个结果的第一个字母,然后像那样创建我的部分吗?是不是最好在一个新的属性中只保留第一个字母,并将其用作sectionNameKeyPath

NSFetchedResultsController *aFetchedResultsController = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:managedObjectContext
            sectionNameKeyPath:@"name.firstLetter"
            cacheName:@"Root"];

谢谢

**编辑:*我不确定这是否有区别,但我的结果是日语,按片假名排序.我想用这些片假名作为节索引.

推荐答案

您应该只传递"name"作为sectionNameKeyPath.请参阅这answer个问题"核心数据备份UITableView与索引".

UPDATE

否则,我同意refulgentis的观点,即瞬态特性是最佳解决方案.此外,在创建NSFetchedResultsController时,sectionNameKeyPath具有以下限制:

如果此密钥路径与

样板文件UITableViewDataSource使用NSFetchedResultsController实现:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[fetchedResultsController sections] count];
}

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

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

// Don't implement this since each "name" is its own section:
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
//    return [sectionInfo name];
//}

UPDATE 2

对于新的"uppercaseFirstLetterOfName"瞬态属性,向模型中的适用实体添加一个新字符串属性,并选中"transient"框.

有几种方法可以实现getter.如果要生成/创建子类,则可以将其添加到子类的实现(.m)文件中.

否则,您可以在NSManagedObject上创建一个类别(我将其放在我的视图控制器的实现文件的顶部,但您可以将其拆分为适当的头文件和它自己的实现文件):

@interface NSManagedObject (FirstLetter)
- (NSString *)uppercaseFirstLetterOfName;
@end

@implementation NSManagedObject (FirstLetter)
- (NSString *)uppercaseFirstLetterOfName {
    [self willAccessValueForKey:@"uppercaseFirstLetterOfName"];
    NSString *aString = [[self valueForKey:@"name"] uppercaseString];

    // support UTF-16:
    NSString *stringToReturn = [aString substringWithRange:[aString rangeOfComposedCharacterSequenceAtIndex:0]];

    // OR no UTF-16 support:
    //NSString *stringToReturn = [aString substringToIndex:1];

    [self didAccessValueForKey:@"uppercaseFirstLetterOfName"];
    return stringToReturn;
}
@end

此外,在此版本中,不要忘记将"uppercaseFirstLetterOfName"作为sectionNameKeyPath传递:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext
sectionNameKeyPath:@"uppercaseFirstLetterOfName" // this key defines the sections
cacheName:@"Root"];

要在UITableViewDataSource实现中取消注释tableView:titleForHeaderInSection::

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

Objective-c相关问答推荐

iPhone 可达性判断

UIButton 标题更改为默认值

Asihttprequest 61 错误

go 掉 UISearchBar 下的 1px 边框

如何使用整数值作为键在 NSMutableDictionary 中设置值?

启动 Finder 窗口并 Select 特定文件

iOS:将 URL 解析为段

适用于 ios 的 360° 全景库

ScrollView 手势识别器吃掉所有的事件

.delegate=self 是什么意思?

如何以编程方式退出 mac 应用程序?

UIKeyboardBoundsUserInfoKey 已弃用,改用什么?

更改 UISearchBar 的字体大小

从 NSMutableString 中删除最后一个字符

如何推送两个视图控制器但只为第二个设置动画过渡?

Objective-C 中的 NSString indexOf

ARC时代的财产与ivar

从带有参数的方法名称创建 Select 器

如何在不管理 UDID 和重新编译的情况下无线分发 ios 应用程序

Block 隐式保留'self';明确提及touch以表明这是预期行为