Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the first character as a section name

I'm using Core Data for a table view, and I'd like to use the first letter of each of my results as the section header (so I can get the section index on the side). Is there a way to do this with the key path? Something like below, where I use name.firstLetter as the sectionNameKeyPath (unfortunately that doesn't work).

Do I have to grab the first letter of each result manually and create my sections like that? Is it better to put in a new property to just hold the first letter and use that as the sectionNameKeyPath?

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

Thanks.

**EDIT: ** I'm not sure if it makes a difference, but my results are Japanese, sorted by Katakana. I want to use these Katakana as the section index.

like image 695
nevan king Avatar asked Nov 16 '09 09:11

nevan king


2 Answers

You should just pass "name" as the sectionNameKeyPath. See this answer to the question "Core Data backed UITableView with indexing".

UPDATE
That solution only works if you only care about having the fast index title scroller. In that case, you would NOT display the section headers. See below for sample code.

Otherwise, I agree with refulgentis that a transient property is the best solution. Also, when creating the NSFetchedResultsController, the sectionNameKeyPath has this limitation:

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.

Boilerplate UITableViewDataSource implementations using 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]; //} 

UPDATE 2

For the new 'uppercaseFirstLetterOfName' transient property, add a new string attribute to the applicable entity in the model and check the "transient" box.

There are a few ways to implement the getter. If you are generating/creating subclasses, then you can add it in the subclass's implementation (.m) file.

Otherwise, you can create a category on NSManagedObject (I put this right at the top of my view controller's implementation file, but you can split it between a proper header and implementation file of its own):

@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 

Also, in this version, don't forget to pass 'uppercaseFirstLetterOfName' as the sectionNameKeyPath:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"uppercaseFirstLetterOfName" // this key defines the sections cacheName:@"Root"]; 

And, to uncomment tableView:titleForHeaderInSection: in the UITableViewDataSource implementation:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {     id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];     return [sectionInfo name]; } 
like image 92
gerry3 Avatar answered Oct 15 '22 22:10

gerry3


There may be a more elegant way to do this, but I recently had the same problem and came up with this solution.

First, I defined a transient property on the objects I was indexing called firstLetterOfName, and wrote the getter into the .m file for the object. e.x.

- (NSString *)uppercaseFirstLetterOfName {     [self willAccessValueForKey:@"uppercaseFirstLetterOfName"];     NSString *stringToReturn = [[self.name uppercaseString] substringToIndex:1];     [self didAccessValueForKey:@"uppercaseFirstLetterOfName"];     return stringToReturn; } 

Next, I set up my fetch request/entities to use this property.

NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:dataContext]; [request setEntity:entity]; [NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [request setSortDescriptors:sortDescriptors]; 

Side note, apropos of nothing: Be careful with NSFetchedResultsController — it's not exactly fully baked yet IMHO, and any situation beyond the simple cases listed in the documentation, you will probably be better off doing it the 'old fashioned' way.

like image 22
refulgentis Avatar answered Oct 15 '22 21:10

refulgentis