【问题标题】:Core Data with A-Z sections带有 A-Z 部分的核心数据
【发布时间】:2009-12-31 18:29:12
【问题描述】:

我正在尝试使用此处的示例为核心数据填充表添加 A-Z 索引:http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/

但是,我无法确定可以在核心数据帮助程序头文件中设置 sectionNameKeyPath 属性的位置:

我需要像在 CoreDataBooks 中那样制作 NSFetchedResultsController 吗?或者我可以在这里的某个地方添加它吗?!抱歉我的无知,任何想法/帮助将不胜感激(这是把我的头发扯掉的第三天)。

+(NSMutableArray *) searchObjectsInContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending : (NSManagedObjectContext *) managedObjectContext
{

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
    [request setEntity:entity]; 

    // If a predicate was passed, pass it to the query
    if(predicate != nil)
    {
        [request setPredicate:predicate];
    }

    // If a sort key was passed, use it for sorting.
    if(sortKey != nil)
    {
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:sortAscending];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [request setSortDescriptors:sortDescriptors];

        [sortDescriptors release];
        [sortDescriptor release];
    }

    NSError *error;

    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    [request release];

    return mutableFetchResults;
}

【问题讨论】:

    标签: iphone core-data


    【解决方案1】:

    您需要实现适当的表视图数据源方法(sectionIndexTitlesForTableView: 和 tableView:sectionForSectionIndexTitle:atIndex:)。

    我建议使用 NSFetchedResultsController 来执行此操作。查看 this answerCore Data backed UITableView with indexing 的示例代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多