iphone - 如何使用第一个字符作为部分名称

我将 Core Data 用于表格 View ,并且我想将每个结果的第一个字母用作部分标题(这样我就可以在侧面获得部分索引)。有没有办法用关键路径做到这一点?如下所示,我使用 name.firstLetter 作为 sectionNameKeyPath (不幸的是,这不起作用)。

我是否必须手动抓取每个结果的第一个字母并像这样创建我的部分?放入一个新属性以仅保存第一个字母并将其用作 sectionNameKeyPath 是否更好?

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

谢谢。

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

最佳答案

您应该只传递“名称”作为 sectionNameKeyPath。看到这个answer到问题“Core Data backed UITableView with indexing”。

更新
该解决方案仅在您只关心拥有快速索引标题滚动器时才有效。在这种情况下,您将不会显示节标题。请参阅下面的示例代码。

否则,我同意 refulgentis 的观点,即 transient 属性是最佳解决方案。此外,在创建 NSFetchedResultsController 时,sectionNameKeyPath有这个限制:

If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings. For example, the first sort descriptor in fetchRequest might specify the key for a persistent property; sectionNameKeyPath might specify a key for a transient property derived from the persistent property.

使用 NSFetchedResultsController 的样板 UITableViewDataSource 实现:

- (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' transient 属性,将新的字符串属性添加到模型中的适用实体并选中“ transient ”框。

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

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

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

https://stackoverflow.com/questions/1741093/

相关文章:

ios - 计算两个 CLLocationCoordinate2D 之间的方位

objective-c - 如何从 C 方法调用 Objective-C 方法?

iphone - 在 Objective-C 中随机化 NSArray 的规范方法

objective-c - 适合 UILabel 中的文本

objective-c - Objective C 中的多个委托(delegate)

ios - 在objective-c中导入swift类,-Swift.h文件未找

iphone - 如何以编程方式区分 iphone 4 和 iphone 4S?

iphone - 什么是-[NSString sizeWithFont :forWidth:line

c - 在 Objective-C 类中混合 C 函数

iphone - animateWithDuration :animations: block ma