To respond to cell selection, you should implement the tableView(_:didSelectRowAt:) method. Because you've already set the ViewController instance as the UITableView delegate, declaring and implementing this method on your ViewController is all you need to do.
The UITableView will automatically call all of the UITableViewDelegate methods that its delegate has implemented. This is why we don't need to do anything more than just implementing tableView(_:didSelectRowAt:). The implementation that we'll write for now is very simple. When a user taps a cell, an alert will be displayed. In Chapter 3, Creating a Contact Detail Page, you will learn how to do something more meaningful like displaying a detail page. The following code should be added to the UITableViewDelegate extension in ViewController.swift:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let contact = contacts[indexPath.row]
let alertController = UIAlertController(title: "Contact tapped", message:
"You tapped (contact.givenName)", preferredStyle: .alert)
let dismissAction = UIAlertAction(title: "Ok", style: .default, handler:
{action in
tableView.deselectRow(at: indexPath, animated: true)
})
alertController.addAction(dismissAction)
present(alertController, animated: true, completion: nil)
}
The preceding code implements the tableView(_:didSelectRowAt:) delegate method. This method receives two arguments, the first argument is an instance of the UITableView that called this method. The second argument is the IndexPath where the selection occurred. Most of the UITableViewDelegate methods are passed these two arguments. In most cases, you will need the indexPath in order to determine the exact action to perform. In the preceding example, the indexPath argument is used to retrieve the contact details that belong to the tapped cell. This information is not read directly from the cell because we don't want to couple the cell's layout directly to the data. If you want to change the cell at a later time, you don't want to have to rewrite a lot of code that is not directly related to the cell itself.
Once the contact data is obtained, a UIAlertController is instantiated. This class is used whenever you want to present an alert to the user. Whenever you create an instance of UIAlertController, you are expected to pass it a title and a message. The third argument you must pass to the UIAlertController initializer is the preferred style. In this case the preferred style is an alert, but you could also use an action sheet as the preferred style.
Once you have created your UIAlertController instance, you should associate at least one action with it. This is done through instances of UIAlertAction. Each action has a title, style, and a completion handler that is called whenever the user selects the action it's associated with. For the example you have just implemented a single dismiss action should suffice. In the completion handler for the dismiss action, tableView.deselectRow(at:animated:) is called. Calling this method makes sure that the selected cell is deselected so it doesn't remain highlighted all the time.
Once the action is configured, it is added to alertController and the alertController is presented on the current UIViewController. If you hit build and run now, you can tap on a cell and you will see the alert modal pop up. Tapping on Ok will dismiss the alert and deselect the selected row.
Even though setting this up wasn't very complex, it's really powerful. The delegation pattern makes it really easy to implement handlers for UITableView's actions without a lot of boilerplate code. You could even write a dedicated class or struct that conforms to UITableViewDelegate and use that as the delegate for your UITableView. This means you are able to split up ViewController and UITableViewDelegate; doing so allows you to reuse the UITableViewDelegate implementations in other view controllers in your app. We won't do that in this chapter, but if you'd like you can try to do it. It will truly help you to gain a deeper understanding of delegation and why it's such a powerful technique.