Assignment #4 Task #10

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

The recents tab must show the list of most recently view photos in chronological order of viewing with the most recent at the top, and no duplicates in the list. It is sufficient to only update the list each time it (re)appears on screen (i.e. in viewWillAppear:). A photo can be uniquely identified by its “id” entry in its dictionary.

Create a model for the recents list, holding data for the recent photos:

@property (nonatomic, strong) NSArray *photos;

Synthesize the model and set its setter to reload the table when the data has changed:

@synthesize photos = _photos;

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

For now – we have not yet implemented the storage of the recent photo data – we just use an empty array:

- (void)viewWillAppear:(BOOL)animated
{
    self.photos = [NSArray array];
}

… only one section:

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

The number of rows in the table equals the number of photos in the array:

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

… and we populate the table cells with the formatted data from the photos in the list:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Recent 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 = [FlickrData titleOfPhoto:photo];
    cell.detailTextLabel.text = [FlickrData subtitleOfPhoto:photo];
    return cell;
}

The actual result from this code is an empty table. To see if it works we need to implement the next task.

The complete code for this task is available at github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.