Removing rows from a table view
As you have learned in Chapter 14, Getting Started with MVC and Table Views, table view row deletion is handled by the tableView(_:commit:forRowAt:)
method, which is one of the methods declared in the UITableViewDataSource
protocol.
You’ll implement this method in the JournalListViewController
class. Follow these steps:
- In the Project navigator, click the JournalListViewController file and add the following code to the
JournalListViewController
class after the existing table view data source methods:func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { journalEntries.remove(at: indexPath.row) tableView.reloadData() } }
This will allow you to swipe left to display a Delete button, and when you tap the Delete button, the corresponding JournalEntry
instance will be removed from the journalEntries...