cs193p – Assignment #6 Extra Task #1

Please note, this blog entry is from a previous course. You might want to check out the current one.

Divide all tables of photos (except maybe the “All” table below) into alphabetical sections. This turns out to be easy in table views driven by Core Data, but only if you have an attribute in the database which is “that photo’s section heading” and which sorts in exactly the same order as the photos themselves sort.

Add another attribute to the Photo entity to hold the first letter of each photo title. Recreate its sub class and adjust its category:

+ (Photo *)photoWithFlickrInfo:(NSDictionary *)photoDictionary
        inManagedObjectContext:(NSManagedObjectContext *)context
{
        ...
        photo.firstLetter = [photo.title substringToIndex:1];
        ...
}


… and adjust the fetched-results controller to use the new attribute as sections:

- (void)setupFetchedResultsController
{
        ...        
        self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                            managedObjectContext:self.tag.managedObjectContext
                                                                              sectionNameKeyPath:@"firstLetter"
                                                                                       cacheName:nil];
    ...
}

The complete code is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

2 thoughts on “cs193p – Assignment #6 Extra Task #1”

  1. I’ve come across a problem with this for instances where the photo title begins with a non-standard character. For example ‘č’.

    This photo will get put under the section ‘c’ but it won’t be counted in the section, which puts all the following sections out of whack.

    If it was just this one case you could deal with it, but it also occurs with japanese characters and symbols like ©.

    How should these be dealt with in terms of section names and section index?

    1. Strange, I assumed iOS should be able to deal with multi-byte characters automatically. I suppose it is more likely a problem of the flickr-fetcher class. Please let us know of your investigation 😉

Leave a Reply

Your email address will not be published.