Chapter 9
Customize Table Views Using Prototype Cell, Diffable Data Source and Dark Mode
I think that's the single best piece of advice: Constantly think about how you could be doing things better and questioning yourself.
- Elon Musk, Tesla Motors
In the previous chapter, we created a simple table-based app to display a list of restaurants using the basic cell style. In this chapter, we'll customize the table cell and make it look more stylish. And, I will show a modern way to handle the data source of a table view. There are a number of changes and enhancements we are going to work on:
- Rebuild the same app using
UITableViewController
instead ofUITableView
- Use the new
UITableViewDiffableDataSource
to configure the data of the table view - Display a distinct image for each restaurant rather than showing the same thumbnail
- Design a custom table view cell instead of using the basic style of table view cell
- Test the app in dark mode
You may wonder why we need to rebuild the same app. There are always more than one way to do things. Previously, we used UITableView
to create the table view. In this chapter, we'll use UITableViewController
and UITableViewDiffableDataSource
to create a table view app. Will it be easier? Yes, it's going to be easier. Recalled that we needed to explicitly adopt both UITableViewDataSource
and UITableViewDelegate
protocols, UITableViewController
has already adopted these protocols and established the connections for us. On top of this, it has all the required layout constraints right out of the box.
The UITableViewDiffableDataSource
class was first introduced in iOS 13, which is a modern way to supply a table view with the data to display. You can still use the traditional way, which was discussed in chapter 8, to populate data into a table. This new approach replaces the UITableViewDataSource
protocol and will make your life much easier to cope with data changes in a table view. As more devices are adopting iOS 13 (or up), the UITableViewDiffableDataSource
approach will become the standard implementation of UITableView
.
Starting from this chapter and onwards, you begin to develop a real-world app called FoodPin. It's gonna be fun!
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.