Chapter 14. Movie Night - iOS App

Congratulations on making it to the final chapter! In this chapter, we will look at one more useful pattern when creating iPhone apps-making lists using the UITableViewController type. Lists are very popular in apps since they are an intuitive way of illustrating a list of items. These items can then be added, deleted, or perhaps you can select them to reveal more information about a specific item.

After looking at how you can create a list for your app, we will use that knowledge to build one last app-the Movie Night application. This application will use some of the knowledge we have gained throughout the book, while taking advantage of the pattern of creating lists. Toward the end of the chapter, we will quickly look at how we can install our application on our iPhone so that we are able to carry the application with us, making it easier to show it to our friends and family.

To sum up, this chapter will cover the following topics:

Creating lists is very common in the real world, as well as in software applications. A list in a software application can be used for a variety of things, such as creating a to-do list, showing a list of the most recent news, or showing the items on the menu of your local pizza joint:

How to create a list

A list in an iOS application is most commonly implemented, and referred to, as a table view. A table view refers to the UITableView UI element, which is the view that gives us a list of horizontal items. Each item in the list is referred to as a cell due to its type being UITableViewCell. A cell comes with a set of predefined formats, but custom cells can also be created to fit the developer's needs. Further, a list can be divided into sections, for example, by the first letter of a contact's last name, as in the preinstalled Contacts app that ships with iOS:

How to create a list

Besides setting up the interface to include the UITableView, a set of methods needs to be implemented in the corresponding UIViewController. These methods will ensure that data is able to be displayed and will tell the application how it should handle user interaction with the table view. In fact, there are two protocols that are very relevant to conform to in order to set up a list. A protocol defines a set of variables and/or a set of functions a conforming object needs to implement in order to satisfy the protocol. In relation to setting up a list, conforming to two protocols is necessary to make it work correctly. These two protocols are named UITableViewDataSource and UITableViewDelegate.

The data source, or the UITableViewDataSource protocol to be more precise, has some required methods that you need to implement in order to conform to the protocol. The protocol also defines a set of optional methods that one can implement for more control of the table view. Let's go through some of the most popular methods.

The first method has the following signature:

func numberOfSections(in tableView: UITableView) -> Int

This method should return a value of the Int type, which should indicate how many sections there are in the table view. If we take a look at the screenshot from Apple’s Contacts app, we can see that this table view has five sections. This method will return 1 by default if you do not implement it.

The next method says something about how many rows exist in each section. The signature looks as follows:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

As the method name indicates, this method will state how many rows should go into each section in the  UITableView . For example, in the preceding screenshot from the Contacts app, there should be one row for the first section. The first section is A and the single row is John Appleseed.

Another central method from the UITableViewDataSource protocol has the following signature:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

This method looks a little different than the others because it does not say anything about the number of sections, rows, or similar that is needed in order to set up the table. Instead, it prepares the actual view for the cell that has the UITableViewCell type, as explained earlier. This means that the view this method returns will be the actual cell for that specific row. Looking at the screenshot of the Contacts app, we can see that each cell is fairly simple and only contains one label that shows the name of the contact.