5.8. Tapping a cell in TableView and Practice exercise
Let's open ViewController.swift file and add the following code to the protocols adoption block:
//
// ViewController.swift
// App5
//
// Created by Sakib Miazi on 5/18/23.
//
//codes omitted...
extension ViewController: UITableViewDelegate, UITableViewDataSource{
//codes omitted...
//MARK: deal with user interaction with a cell...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(self.expenses[indexPath.row])
}
}We added the tableView() method for didSelectRowAt. It is the call-back method if a user taps on a cell (selected a cell) in TableView. After you add this method, let's run the app. It should print the corresponding data.

Now, you can see that you can handle it if a user taps on a cell. It's time for exercise.
Exercise
Now your task is to build a third screen, "DisplayExpense," to show the details of an expense if a user taps on it. It could be just three Labels to display the title, amount, and type of the selected expense.
Previous5.7. Second screen, part 3: Send new expense back to ViewController and update the TableViewNext5.9. Reference Code
Last updated
Was this helpful?