Assignment #4 Task #2

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

Create a UITabBarController-based user-interface with two tabs. The first shows a UITableView with the list of places (in alphabetical order) obtained in Required Task #1. The second shows a UITableView with a list of the 20 most recently viewed photos.

Go to the iPhone storyboard, remove the existing view controller and add a tab bar controller. Remove the two new view controllers by replacing them with table view controllers and wire them up using relationship segues. Set the identifier of the tab bar item of the first controller to “Top Rated”, the second one to “Recents”. Change the style of the table cells to “Subtitle” and set the identifier of the cell of the first controller to “Top Places Cell” and of the second one to “Recent Photos Cell”

Next create two new subclasses of UITableViewController for those two table view controllers (File -> New -> File… -> Objective-C class) and use them in storyboard as class for the table view controllers.

Create a model in TopPlacesTableViewController.h – an array holding the top places:

@property (nonatomic, strong) NSArray *places;

Synthesize it and adjust is setter to sort the array and reload the table view when on screen:

@synthesize places = _places;

- (void)setPlaces:(NSArray *)places
{
    if (places == _places) return;
    _places = [places sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString *string1 = [obj1 objectForKey:FLICKR_PLACE_NAME];
        NSString *string2 = [obj2 objectForKey:FLICKR_PLACE_NAME];
        return [string1 localizedCompare:string2];                
    }];
    if (self.tableView.window) [self.tableView reloadData];    
}

Fill the array with data (currently we do not care if the user interface gets blocked):

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.places = [FlickrFetcher topPlaces];
    //NSLog(@"%@", self.places);
}

We do not use sections yet:

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

The number of rows in the table equals the number of objects in our data array:

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

And for now we fill the the cell with the unformatted name of the place:

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

The recents tab will be populated in Task #10, thus we keep it empty for now.

The complete code for this task is available at github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.