Assignment #4 Task #3

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

Anywhere a place appears in a table view in your application, the most detailed part of the location (e.g. the city name) should be the title of the table view’s cell and the rest of the name of the location (e.g. state, province, country, etc.) should appear as the subtitle of the table view cell.

The task hints that we will have to use this functionality somewhere else, too. Thus we create two helper functions for the title and the subtitle of the table cells. For both functions we look for the place identifier and cut it into pieces. For the title we take the first piece. For the subtitle we create a new array only using the other pieces and paste them together again:

- (NSString *)titleOfPlace:(NSDictionary *)place
    return [[[place objectForKey:FLICKR_PLACE_NAME] 
             componentsSeparatedByString:@", "] objectAtIndex:0];

- (NSString *)subtitleOfPlace:(NSDictionary *)place
    NSArray *nameParts = [[place objectForKey:FLICKR_PLACE_NAME] 
                          componentsSeparatedByString:@", "];
    NSRange range;
    range.location = 1;
    range.length = [nameParts count] - 1;
    return [[nameParts subarrayWithRange:range] componentsJoinedByString:@", "];

Finally we use these functions to populate our cells:

    cell.textLabel.text = [self titleOfPlace:place];
    cell.detailTextLabel.text = [self subtitleOfPlace:place];

The complete code for this task is available at github.


Leave a Reply

Your email address will not be published.