Displaying Alerts with UIAlertController and Handling Table View Selection
There is no learning without trying lots of ideas and failing lots of times.
- Jonathan Ive
Are you able to complete the previous exercise and create the custom table view? No worries if you can't. I will go through the solution with you in this chapter and introduce some new layout techniques. Up till now, we only focus on displaying data in a table view. I guess you may wonder how we can interact with the table view and detect row selections. This is what we will also discuss in this chapter.
To begin, first download the complete project we built in the previous chapter (http://www.appcoda.com/resources/swift53/FoodPinCustomTable.zip). We will continue to enhance the app and make it even better. In brief, here are what we are going to implement:
- Add another prototype cell to the table view
- Bring up a menu when a user taps a cell. The menu offers two options: Reserve a table and Mark as favorite.
- Display a heart icon when a user selects Mark as favorite.
Through implementing these new features, you will also learn how to use two prototype cells and use
UIAlertController to display alerts in iOS.
Quick note: This class replaces the UIActionSheet and UIAlertView classes for displaying alerts in iOS 8 (or up).
Building a more Elegant Cell Layout
Previously, I had an exercise for you and asked you to redesign the prototype cell to make the cell layout look like that shown in figure 10-2. I hope you already tried to figure out the solution. Even if you couldn't find a way to design the cell, I appreciate your effort. It is not an easy exercise for beginners.
Now let's see how to design the cell layout. Assuming you have already downloaded the project and open it in Xcode, select
Main.storyboard and switch over to Interface Builder. Instead of deleting the existing cell layout, we will create another prototype cell for the new design. That's right! You can create multiple prototype cells in the same table view. The only requirement is that each of the prototype cell should have a distinct identifier.
To 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.