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

问题描述

在iPhone上学习核心数据。看来几个例子Core Data填充一个表视图与部分。 示例使用部分,但是他们'从模型中的完整字符串重新生成。我想把核心数据表组织成一个姓氏的第一个字母,一个地址簿。



我可以进入并创建另一个属性,即一个单一的字母,为每个人为了作为部门分裂,但这似乎kludgy。



这里是我开始...窍门似乎愚弄 sectionNameKeyPath

   - (NSFetchedResultsController *)fetchedResultsController {
// ans。一些STUFF已删除
//根据需要编辑排序键。
NSSortDescriptor * orderDescriptor = [[NSSortDescriptor alloc] initWithKey:@personNameascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:orderDescriptor,nil];

[fetchRequest setSortDescriptors:sortDescriptors];
//如果适当,编辑段名称键路径和缓存名称。
//节的名称键路径的nil表示没有节。
NSFetchedResultsController * aFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@personNamecacheName:@Root];
// ....
}


解决方案>

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

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

现在稍后, FRC:

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

这是我最喜欢的方法。更清洁/更容易实施。此外,您不必对对象模型类进行任何更改以支持它。这意味着它将适用于任何对象模型,前提是节名称指向基于NSString的属性


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.

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"];
//....
}
解决方案

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

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

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,带有由字符串的第一个字母创建的节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:50