Let's build a countdown app using diffable data source.
Our countdown app will start from 10 and decrement the initial value by 1 every second and add the new value as a row in the table view. Throughout the app we will make use of the snapshot as we update the table view and apply the changes.
- Create an Xcode project named Countdown.
- Navigate to the ViewController.swift file and add the following:
enum Section {
case main
}
private var tableView: UITableView!
private var dataSource: UITableViewDiffableDataSource<Section, Int>!
private var timer: Timer!
private var startInterval = 10 // seconds - Configure the table view.
- Configure the data source.
- Configure the timer.
- Add the
startCountdownmethod. - Add the
decrementCountermethod. - Add the
shipmethod. - Add a
refreshbar button item to restart countdown.
