Therefore, as stated earlier, to get data, we need a location. To get the location, we need to get it from the LocationViewController. When a location is selected, we will show a checkmark. We will need this checkmark to update each time a new item is set. Finally, when the Done button is tapped, we need to pass this location to ExploreViewController.
Let's update our LocationViewController first. We need a variable to keep track of the selected location. Add the following inside the LocationViewController.swift file, under the constant manager:
var selectedCity:String?
Then, we need to create a new extension for UITableViewDelegate, as follows. Add the following after our UITableViewDataSource extension:
//MARK: UITableViewDelegate
extension LocationViewController: UITableViewDelegate {
}
As we discussed earlier in the book, delegates supply the behavior. Here, we want a behavior for when the user selects a Table View row and another behavior for when the user deselects the row. First, let's add the selection behavior into our new extension by adding the following code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
selectedCity = manager.locationItem(at:indexPath)
tableView.reloadData()
}
}
Here, we will get the cell of the selected row and set its accessoryType to a checkmark. Then, we will get the location and set it to the selectedCity variable. To only see the checkmark in our Table View cell, we need to remove the disclosure arrow and gray cell selection. Let's update this by doing the following:
- Open Explore.storyboard.
- Select the Table View locationCell in the Location View Controller.
- Select the Attributes inspector in the Utilities panel, and update the Selection field from Gray to None.
- Next, update the Accessory field from Disclosure Indicator to None.