Assignment #4 Task #4

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

When the user chooses a place from the list obtained in Required Task #1, you must query Flickr again to get an array of 50 recent photos from that place and display them in a list. Do this using the FlickrFetcher method photosInPlace:maxResults: (it returns an array of dictionaries, each of which contains info about a photo).

In the iPhone storyboard we add a new table view controller, add a new class for it and set it in storyboard. From the cell inside the top-places table view controller create a push segue to the new table view controller. Don’t forget to set the style and the reuse identifier of the cell of the new table view controller.

Some of what we have to do for the new table, we have already done for the old one. Some of we will have to do for this table we will need for the recently-viewed-photos table. Thus we should create a new table-view-controller class, put the common parts there and make it the super class of our other table view classes.

In the header file of the new table-view-controller class we add a model to hold the data for the photos as well as of the selected place:

@property (nonatomic, strong) NSDictionary *place;
@property (nonatomic, strong) NSArray *photos;

Now synthesize them and adjust the setter for the photos to reload the table when on screen:

@synthesize place = _place;
@synthesize photos = _photos;

- (void)setPhotos:(NSArray *)photos
{
    if (_photos == photos) return;
    _photos = photos;
    if (self.tableView.window) [self.tableView reloadData];
}

Populate the photos of a specific place from Flickr:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = [self titleOfPlace:self.place];    
    self.photos = [FlickrFetcher photosInPlace:self.place maxResults:NUMBER_OF_PHOTOS];
}

For the table we do not use sections yet:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

The number of rows equals the number of photos:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.photos count];
}

For now we populate the cell with the unformatted photo title:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Top Photos Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                             reuseIdentifier:CellIdentifier];
    
    NSDictionary *photo = [self.photos objectAtIndex:indexPath.row];
    cell.textLabel.text = [photo objectForKey:FLICKR_PHOTO_TITLE];
    cell.detailTextLabel.text = @"";
    return cell;
}

Finally we provide the place in the segue by looking for the index of the selected row:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    if ([segue.identifier isEqualToString:@"Show Photos from Place"]) {
        [segue.destinationViewController setPlace:
         [self.places objectAtIndex:
          [self.tableView indexPathForSelectedRow].row]];        
    }
}

The complete code for this task is available at github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.