views:

227

answers:

1

Hello,

I am trying to add an A-Z index for a core-data populated table using the example here: http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/

However, I can't work out where I can set my sectionNameKeyPath property in the core data helper header file:

Do I need to make a NSFetchedResultsController like they do in CoreDataBooks? Or can I just add it in somewhere here?! Sorry for the ignorance, any thoughts / help would be much appreciated (this is day 3 of tearing my hair out over it).

+(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;
}
+2  A: 

You need to implement the appropriate table view datasource methods (sectionIndexTitlesForTableView: and tableView:sectionForSectionIndexTitle:atIndex:).

I would recommend using an NSFetchedResultsController to do this. Check out this answer to Core Data backed UITableView with indexing for example code.

gerry3