Sunday, December 7, 2014

Storyboard - Part II - Prototype Cells


Prototype cells allows one to design custom layout for the table view cells from the storyboard editor itself.  The table view controller comes by default with a blank prototype cell. From the attribute inspector, one can select the Style to Subtitle and this includes two labels in the cell. However, running the app doesn't yet show this cell as the data is not yet available for displaying the cells. 

Since the sample decides to use Custom cell type, below few additional items are to be done. The style attribute needs to be set to None. The row height set to 55. 

The main work was in AppDelegate. The main idea was to link the data from AppDelegate to the table view controller. The table view controller was inside a navigation container controller and which is inside the Tab bar container controller. 
Below is the code to access the table view controller from app delegate in this code. 

Tabbar controller is the root view controller in the application. 

UITabBarController *tabcontroller = (UITabBarController) self.window.rootViewController; 
UINavigationController *navController  = [tabController viewCotnrollers][0]; // this fetches the navigation controller at the tab 0. This assumes that the navigation controller is at tab 0. 
PlayersViewController *playerViewController = [navController viewcontrollers][0];
playerViewController.players = _players; 

Inside the PlayersViewCotroller, the cell for row index path needs a bit of modification to give the cell identifier as the identifier we gave in the Prototype Cell.  

static NSString *CellIdentifier = @"PlayerCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    Player *player = (self.players)[indexPath.row];
    cell.textLabel.text = player.name;
    cell.detailTextLabel.text = player.game;

Thats it! . Its all done. 

References: 

No comments:

Post a Comment