Chapter 19. Displaying details of your LioN

 

This chapter covers

  • Optionals
  • Typecasting
  • Optional binding

If you open the Contacts app on your iPhone, you’ll notice that your contacts are displayed using a table view, and individual contacts are in a tableView cell. If you select one of those cells, another page loads with the details of that contact, and you can edit the contact if you choose. You’re going to add the same type of functionality to your LioN app now.

19.1. Capturing the tapped row index

The first thing you need to do in order to display the details of a specific row is to capture which row the user tapped. If only there was a function that already captured this information! Oh, wait—there is. Open your MainViewController again, and start typing (outside of any other functions but still within the class) the following:

override func tableV

Xcode’s auto-complete function will now display the functions that are available for you to select, as shown in figure 19.1. Let Xcode fill in the auto-complete data and add the didSelectRowAtIndexPath function to your code.

Figure 19.1. Xcode uses auto-complete to display the available functions for the tableView. Select the didSelectRowAtIndexPath function.

Next you’ll add a line to print out which row the user tapped to make sure you’re capturing it correctly. Add the following line:

print(lionData[indexPath.row].lionName)

19.2. Adding a detail page to the storyboard

19.3. Passing data to the DetailViewController

19.4. Summary