Chapter 9 UITableView and UITableViewController

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.

screen-shot-2016-10-21-at-12-27-32-am

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.

screen-shot-2016-10-21-at-12-27-32-am

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.

screen-shot-2016-10-21-at-3-45-13-am

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.

screen-shot-2016-10-21-at-4-05-22-am


UITableView’s Data Source

Create a new Swift file named ItemStore.

screen-shot-2016-10-21-at-4-12-36-am


Giving the controller access to the store

In ItemViewController.swift add:

screen-shot-2016-10-21-at-4-16-00-am

In AppDelegate.swift add:

screen-shot-2016-10-21-at-4-14-52-am

In ItemStore.swift add:

screen-shot-2016-10-21-at-4-16-41-am

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:

screen-shot-2016-10-21-at-4-27-27-am


Creating and retrieving UITableViewCells

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

screen-shot-2016-10-21-at-4-32-29-am

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:

screen-shot-2016-10-21-at-4-32-56-am

Content Insets

In ItemsViewController.swift add:

screen-shot-2016-10-21-at-5-09-55-am

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.

css.php