How to Show Columnar Data in iOS Apps - dummies

How to Show Columnar Data in iOS Apps

By Rajiv Ramnath

The Table view in an iOS app is a complex view needing complex delegates and often shows data in a columnar display. It presents data in multiple rows, with each row divided into columns. You can see a Table view in Tic-Tac-Toe in the Manage Tic-Tac-Toe Users screen, as shown below.


You can add a Table view as you do a view in a storyboard. You can also set several attributes of the view in the storyboard via the Attributes inspector. However, in order for content to appear in a table, it must have a data source delegate — that is, a class that implements the UITableViewDataSource protocol.

For the table shown in the figure below, the data source delegate is the User Manager view controller (implemented in TTTUserManagerViewController.m and .h). This delegate is also configured via the storyboard, as shown above.


The following code shows the cellForRowAtIndexPath data source method:

#pragma Table View Data Source methods
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *userTableIdentifier = @"UserItem";
  UITableViewCell *cell =
   [tableView dequeueReusableCellWithIdentifier:userTableIdentifier];
  if (cell == nil) {
   cell = [[UITableViewCell alloc]
  cell.textLabel.text =
   [[userTableData objectAtIndex:indexPath.row] userid];
  return cell;
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return [userTableData count];

In the preceding code, the key method in the TableViewDelegate protocol (the one that specifies how interactions with the Table view are handled) is the method didSelectRowAtIndexPath, as shown next. The content of the selected row (that is, the userid of the user) is read from the selected cell. Utilizing the userid, the user object is looked up and cached in the member variable selectedUser.

#pragma Table View Delegate methods
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  NSString *cellText = cell.textLabel.text;
  selectedUser = [userManager getUserGivenUserid:cellText];