Please note, this blog entry is from a previous course. You might want to check out the current one.
Any list of photos should display the photo’s title as the table view cell’s title and its description as the table view cell’s subtitle. If the photo has no title, use its description as the title. If it has no title or description, use “Unknown” as the title. Flickr photo dictionary keys are #defined in FlickrFetcher.h.
Create two new helper methods. For the title if there is one return the title photo, otherwise if there is a description return it, or if both do not exist return “Unkown”. For the subtitle check the title first. If is set to “Unkown” or the description of the photo, return an empty string. Otherwise return the description:
#define FLICKR_UNKNOWN_PHOTO_TITLE @"Unknown" + (NSString *)titleOfPhoto:(NSDictionary *)photo { NSString *title = [photo valueForKeyPath:FLICKR_PHOTO_TITLE]; if ([title length]) return title; title = [photo valueForKeyPath:FLICKR_PHOTO_DESCRIPTION]; if ([title length]) return title; return FLICKR_UNKNOWN_PHOTO_TITLE; } + (NSString *)subtitleOfPhoto:(NSDictionary *)photo { NSString *title = [FlickrHelper titleOfPhoto:photo]; if ([title isEqualToString:FLICKR_UNKNOWN_PHOTO_TITLE]) return @""; NSString *subtitle = [photo valueForKeyPath:FLICKR_PHOTO_DESCRIPTION]; if ([title isEqualToString:subtitle]) return @""; return subtitle; }
Use those helper methods to set the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.textLabel.text = [FlickrHelper titleOfPhoto:photo]; cell.detailTextLabel.text = [FlickrHelper subtitleOfPhoto:photo]; ... }
The complete code is available on github.