UITableViewController

A UITableView is a view object. Recall that in the MVC design pattern, which iOS developers do their best to follow, each class falls into exactly one of the following categories:

  • model: holds data and knows nothing about the UI

  • view: is visible to the user and knows nothing about the model objects

  • controller: keeps the UI and the model objects in sync and controls the flow of the application

As a view object, a UITableView does not handle application logic or data. When using a UITableView, you must consider what else is necessary to get the table working in your application:

  • A UITableView typically needs a view controller to handle its appearance on the screen.

  • A UITableView needs a data source. A UITableView asks its data source for the number of rows to display, the data to be shown in those rows, and other tidbits that make a UITableView a useful UI. Without a data source, a table view is just an empty container. The dataSource for a UITableView can be any type of object as long as it conforms to the UITableViewDataSource protocol.

  • A UITableView typically needs a delegate that can inform other objects of events involving the UITableView. The delegate can be any object as long as it conforms to the UITableViewDelegate protocol.

An instance of the class UITableViewController can fill all three roles: view controller, data source, and delegate. In the MVC pattern, all these roles are fulfilled by controller objects.

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

When a UITableViewController creates its view, the dataSource and delegate properties of the UITableView are automatically set to point at the UITableViewController (Figure 9.4).

Figure 9.4  UITableViewController-UITableView relationship

UITableViewController-UITableView relationship

Subclassing UITableViewController

You are going to implement a subclass of UITableViewController for LootLogger. Create a new Swift file named ItemsViewController. In ItemsViewController.swift, define a UITableViewController subclass named ItemsViewController.

Listing 9.1  Creating the ItemsViewController class (ItemsViewController.swift)

import Foundation
import UIKit

class ItemsViewController: UITableViewController {

}

Now open Main.storyboard. You want the initial view controller to be a table view controller. Select the existing View Controller and press Delete. Then drag a Table View Controller from the library onto the canvas. With the Table View Controller selected, open its identity inspector and change the class to ItemsViewController. Finally, open the attributes inspector for Items View Controller and check the Is Initial View Controller checkbox.

Build and run your application. You should see an empty table view, as shown in Figure 9.5. As a subclass of UIViewController, a UITableViewController inherits the view property. When this property is accessed for the first time, the loadView() method is called, which creates and loads a view object. A UITableViewController’s view is always an instance of UITableView, so asking for the view of a UITableViewController gets you a bright, shiny, and empty table view.

Figure 9.5  Empty UITableView

Empty UITableView

You no longer need the ViewController.swift file that the template created for you. Select this file in the project navigator and press Delete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset