Chapter 9 UITableView and UITableViewController


Model – holds data and knows nothing about the user interface.

View – is visible to the user and knows nothing about the model objects.

Controller – keeps the user interface and the model objects in sync and controls the flow of the application.

UITableView is a view object and doesn’t handle application logic or data. It typically needs a view controller to handle its appearance on the screen. I needs a data source. UITableView asks the data source for the the number of rows to display and the data to be shown. Without the data source the UITableView is an empty container. Any type of object can be a data source if it conforms to the UITableViewDataSource protocol.

An instance of UITableViewController class can be a view controller, data source, and a delegate.

UITableViewController is a subclass of UIViewController. UITableViewController’s view is always an instance of UITableView and the UITableViewController handles the preparation and presentation of the UITableView.

Subclassing UITableViewController

Implementing a subclass of UITableViewController for Homepwner. Create a new file named ItemsViewController. Define UITableViewController subclass named ItemsViewController.


In Main.storyboard, delete the View Controller. Drag a Table View Controller onto the canvas. In the identity inspector change the class to ItemViewController. In the attributes inspector, check the box for Initial View Controller. Then delete the ViewController.swift file.

Creating the Item Class

Create a new Item.swift file.


Item inherits from NSObject (base class that most Objective-C classes inherit from). SerialNumber is an optional String because an item doesn’t have to have a value.

Custom initializers

Structs do not support inheritance. Classes can have two kinds of initializers: designated initializers and convenience initializers. Every class has at least one designated initializer.

Designated initializer – ensures all properties in a class have a value.


Free initializer – init() is useful when all your class properties have default values and do not need new instances.

Convenience initializers – always call another initializer on the same class.


UITableView’s Data Source

Create a new Swift file named ItemStore.


Giving the controller access to the store

In ItemViewController.swift add:


In AppDelegate.swift add:


In ItemStore.swift add:


Dependency inversion principle – goal is to decouple objects in inverting dependencies between them. States that high-level objects should not depend on low-level objects, both should depend on abstraction. Also states that abstractions should not depend on details, details should depend on abstractions.

Dependency injection – high-level objects do not assume which low-level objects they need to use, instead they are passed through an initializer or property.

Implementing data source methods

In ItemsViewController.swift add:


Creating and retrieving UITableViewCells

In ItemsViewController.swift add: this will display the contents in allItems array


Reusing UITableViewCells

iOS devices have a limited amount of memory, reusing cells saves space. Reusing cells essentially tells the table view  to return a cell with the same class.

In Main.storyboard, select the prototype cell and open the attributes inspector. Change style to Right detail and give it an identifier of UITableViewCell.

In ItemsViewController.swift add:


Content Insets

In ItemsViewController.swift add:


So far, the application has been taking up the entire screen not leaving room for the status bar. This cade snippet adds padding at the top of the screen.