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.

Let’s start.

 

We are starting with creating the Single View Application in our Xcode IDE.

Screen Shot 2016-07-11 at 11.09.50

 

Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.

Screen Shot 2016-08-28 at 18.20.27

 

Your project should look like this after finishing previous step:

Screen Shot 2016-08-28 at 18.20.55

 

We are setting the size and we are choosing compact, portrait for all iPhones.

Screen Shot 2016-08-28 at 18.21.41

 

From Object Library we are taking UITableViewController component.

Screen Shot 2016-08-28 at 18.22.24

 

As we can see on the image below this component comes with UITableViewCell prototype cell which we need only to configure.

Screen Shot 2016-08-28 at 18.22.39

 

Before configuring the cell we will set this controller to be initial so can be displayed right after application start.

Screen Shot 2016-08-28 at 18.23.53

 

We can see that the pointer which show initial view controller now is automatically positioned to our new controller.

Screen Shot 2016-08-28 at 18.24.04

 

We are selecting and removing existing view controller.

Screen Shot 2016-08-28 at 18.24.13

 

Now we are creating new Cocoa Touch Class which will be used for presenting data.

Screen Shot 2016-08-28 at 18.25.04

 

 

Screen Shot 2016-08-28 at 18.25.13

 

For Subclass we are selecting UITableViewController because we want to have already implemented basic methods.

Screen Shot 2016-08-28 at 18.25.42

 

 

Screen Shot 2016-08-28 at 18.25.47

 

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.

Screen Shot 2016-08-28 at 18.26.03

 

We must to select which is the File Owner so in the Identity Inspector for Custom Class we are selecting newly created file.

Screen Shot 2016-08-28 at 18.26.21

 

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.

Screen Shot 2016-08-28 at 18.27.02

 

For cell reusability we are setting cell Identifier.

Screen Shot 2016-08-28 at 18.27.08

 

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.

Screen Shot 2016-08-28 at 18.27.40

 

After deleting the class ViewController our Project Navigator should look as on the image bellow with only AppDelegate and TutorialTableViewController as controller.

Screen Shot 2016-08-28 at 18.27.51

 

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.

Screen Shot 2016-08-28 at 19.04.50

 

When we compile the application we can see that the string from the data array are listed as rows in our UITableView.

Screen Shot 2016-08-28 at 18.34.13

 

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.

Print Friendly

Leave a Comment

Your email address will not be published. Required fields are marked *