How to Create a Table View in Your iOS App

By Neal Goldstein, Dave Wilson

Although powerful in the iOS app world, Table views are surprisingly easy to work with. To create a Table view, you follow only four — count ’em, four — steps, in the following order:

  1. Create and format the view itself.

    This includes specifying the Table style and a few other parameters, most of which you do in Interface Builder.

  2. Specify the Table view configuration.

    Not too complicated, actually. You let UITableView know how many sections you want, how many rows you want in each section, and what you want to call your section headers. You do that with the help of the numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, and tableView:titleForHeaderInSection:methods, respectively.

  3. Supply the text (or graphic) for each row.

    You return that from the implementation of the tableview:cellForRowAtIndexPath method. This message is sent for each visible row in the Table view, and you return a Table View cell to display the text or graphic.

  4. Respond to a user selection of the row.

    You use the tableView:didSelectRowAtIndexPath: method to take care of this task. In this method, you can create a view controller and push it onto the stack (as the storyboard does in a segue), or you can even send a message to the controller that presented a Modal View controller (or any other object).

A UITableView object must have a data source and a delegate:

  • The data source supplies the content for the Table view.

  • The delegate manages the appearance and behavior of the Table view.

The data source adopts the UITableViewDataSource protocol, and the delegate adopts the UITableViewDelegate protocol — no surprises there. Of the preceding methods, only tableView:didSelectRowAtIndexPath: is included in the UITableViewDelegate protocol. All the other methods are included in the UITableViewDataSource protocol.

The data source and the delegate are often (but not necessarily) implemented in the same object, which is often a subclass of UITableViewController. UITableViewController adopts the necessary protocols and even furnishes some method stubs for you. In this case, the Table view is just another object in the DestinationCOntroller view.

There’s another way to display a label such as Pick a Place using a UITableViewController. UITableView has a tableHeaderView property which is a view. You could create a view with the label, one or more images, and maybe another label and then assign that view to tableHeaderView in a UITableView either standing alone as is the case here or situated within a UITableViewController.

Implementing these five (count ’em, five) methods (in the four steps earlier) is all you need to do to implement a Table view.

Not bad.

Make sure you adopt the Table View delegate and Data Source protocols.

Add the bolded code to the DestinationController.m file’s viewDidLoad method.

- (void)viewDidLoad
[super viewDidLoad];
self.destinationTableView.delegate = self;
self.destinationTableView.dataSource = self;

As you might surmise, this makes the both DestinationController the delegate and the data source.