As i mention in my previous post UITableView Basics Part One where we saw how easy is to implement listing functionality in your application using UITableView, we will continue with more tutorials for presenting different ways of using this powerful component and at the end of this UITableView tutorial series we will design and develop one real world application which can be your first useful application for publishing.
In this tutorial instead of using UITableView as view in our UIViewController we will use one controller that extends UIViewController with UITableView predefined methods. The name of this controller is UITabeViewController.
Because this tutorial is very similar to the previous UITableView tutorial we are implement only most important method just to display listing the data from our strings array.
We are starting with creating the Single View Application in our Xcode IDE.
Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.
Your project should look like this after finishing previous step:
We are setting the size and we are choosing compact, portrait for all iPhones.
From Object Library we are taking UITableViewController component.
As we can see on the image below this component comes with UITableViewCell prototype cell which we need only to configure.
Before configuring the cell we will set this controller to be initial so can be displayed right after application start.
We can see that the pointer which show initial view controller now is automatically positioned to our new controller.
We are selecting and removing existing view controller.
Now we are creating new Cocoa Touch Class which will be used for presenting data.
For Subclass we are selecting UITableViewController because we want to have already implemented basic methods.
After creating the new file our Project Navigator should look like on image bellow. We can see that the content of the file is pretty much same as in our previous tutorial where implemented this default methods from the UITableViewDataSource and UITableViewDelegate manually. Also here we can see that we are not writing UITableViewDataSource and UITableViewDelegate in class declaration because this class already conform with this two protocols.
We must to select which is the File Owner so in the Identity Inspector for Custom Class we are selecting newly created file.
Then we are selecting the prototype cell which is already as part of out UITableViewController and we are cell style, in this case is Basic.
For cell reusability we are setting cell Identifier.
When we finish with previous steps, we are selecting class ViewController in out Project Navigator and we are deleting because is not needed any more.
After deleting the class ViewController our Project Navigator should look as on the image bellow with only AppDelegate and TutorialTableViewController as controller.
As in previous tutorial we are defining data array with strings we want to display. Next we are initializing our UITableView with number of sections which in this case is 1 and the number of rows in section which is also number of elements in our array with strings.
In the most important method cellForRowAtIndexPath, we are loading from the tableView reusable cell with cell identifier which we set previously, in this case “Cell” and on its label we are setting the specific text for the row index from the array with strings.
When we compile the application we can see that the string from the data array are listed as rows in our UITableView.
As we saw here in this tutorial, using UITableViewController in our application instead of manually adding UITableView have also benefits because it give us automatically UITableViewCell which we need just to configure, default implementation on most important methods in the class and also already conform with the protocols so we need only to write what we want to display.
Complete source code can be found on my GitHub ready for testing.