Please note, this blog entry is from a previous course. You might want to check out the current one.
When the user chooses one of the tags in the list, navigate to a new list of the titles of all the photos (in the data you queried originally with stanfordPhotos) that have that tag. The subtitles in this list should be the photo’s description (if it has one).
This task is similar to the previous one (and you could safe some time by reusing the code from shutterbug).
Create a new table-view-controller sub class. As model use an array to hold the photos – note, this time it has to be public, as the other view controller needs to set it.
@property (nonatomic, strong) NSArray *photos; // of NSDictionary
When the photos are set, reload the table:
- (void)setPhotos:(NSArray *)photos { _photos = photos; [self.tableView reloadData]; }
… use them to populate the table:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.photos count]; } - (NSString *)titleForRow:(NSUInteger)row { return [self.photos[row][FLICKR_PHOTO_TITLE] description]; } - (NSString *)subtitleForRow:(NSUInteger)row { return [self.photos[row][FLICKR_PHOTO_OWNER] description]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Flickr Photo"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.text = [self titleForRow:indexPath.row]; cell.detailTextLabel.text = [self subtitleForRow:indexPath.row]; return cell; }
As described above, the public property is set by the table view controller from the previous task:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([sender isKindOfClass:[UITableViewCell class]]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; if (indexPath) { if ([segue.identifier isEqualToString:@"Show Photos"]) { if ([segue.destinationViewController respondsToSelector:@selector(setPhotos:)]) { NSString *tag = [self tagForRow:indexPath.row]; [segue.destinationViewController performSelector:@selector(setPhotos:) withObject:self.photosByTag[tag]]; [segue.destinationViewController setTitle:[tag capitalizedString]]; } } } } }
… where we added a new helper method (which also is used when generating the cell):
- (NSString *)tagForRow:(NSUInteger)row { return [[self.photosByTag allKeys] objectAtIndex:row]; }
Finally drag out a new table-view controller in story board. Set its class to the newly created one. Change the cell type to “subtitle” and its reuse identifier to “Flickr Photo”. Select the cell of the first table and control drag to the new table to create a new push segue. Set its identifier to “Show Photos”:
The complete code is available on github.