您应该只传递“name”作为sectionNameKeyPath。查看此answer的问题“带有索引的核心数据支持的UITableView”。
UPDATE
也就是说,如果你只关心具有快速索引标题滚动解决方案仅适用。在这种情况下,你不会显示节标题。请参阅下面的示例代码。
否则,我同意refulgentis一个瞬态属性是最好的解决方案。此外,在创建NSFetchedResultsController时,该sectionNameKeyPath有此限制:
如果这个关键路径是不一样的,通过在fetchRequest第一个排序 描述符中指定 ,他们必须 产生相同的相对排序。例如,fetchRequest中的第一个排序描述符 可能为持久性属性指定密钥 ; 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];
//}
更新2
对于新的 'uppercaseFirstLetterOfName' 瞬态特性,在模型中,并检查一个新字符串属性添加到相关实体“瞬态”框。
有几种实现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];
}
我试着把名字放在'sectionNameKeyPath'中,但是每个返回的名字都有一段。我获得了与名称一样多的部分,每部分只有一个条目(名称)。 – 2009-11-16 10:19:23
您可能没有正确安装。再读一个答案。它像广告一样工作。 – 2009-11-16 10:33:34
我不相信答案本身就是“有效的”,我不知道为什么回应者将第一个名字作为关键路径的行为是一个奇迹。 – refulgentis 2009-11-16 10:36:28