Chapter 10
Interacting with Table Views and Using UIAlertController
There is no learning without trying lots of ideas and failing lots of times.
- Jonathan Ive
Up till now, we only focus on displaying data in a table view. I guess you are thinking how we can interact with the table view and detect row selections. This is what we're going to discuss in this chapter.
We'll continue to polish the FoodPin app, which we have built in the previous chapter, and add a couple of enhancements:
- Bring up a menu when a user taps a cell. The menu offers two options: Call and Check-in.
- Display a heart icon when a user selects Check-in.
Through implementing these new features, you will also learn how to use UIAlertController, which is commonly used to display alerts in iOS apps.
Quick note: This class replaces the UIActionSheet and UIAlertView classes for displaying alerts in iOS 8 (or up).
Understanding the UITableViewDelegate Protocol
When we first built the SimpleTable app in Chapter 8, we adopted two delegates, UITableViewDelegate
and UITableViewDataSource
, in the RestaurantTableViewController
class. I have discussed with you the UITableViewDataSource
protocol but barely mentioned about the UITableViewDelegate
protocol.
As said before, the delegate pattern is very common in iOS programming. Each delegate is responsible for a specific role or task to keep the system simple and clean. Whenever an object needs to perform a certain task, it depends on another object to handle it. This is usually known as "separation of concerns" in software design.
The UITableView
class applies this design concept. The two protocols are designed for different purposes. The UITableViewDataSource
protocol defines methods, which are used for managing table data. It relies on the delegate to provide the table data. On the other hand, the UITableViewDelegate
protocol is responsible for setting the section headings and footers of the table view, as well as, handling cell selections and cell reordering.
To manage the row selection, we will implement some of the methods in the UITableViewDelegate
protocol.
To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.