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 UITableViewDelegate
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 theJournalListViewController
class after the table view data source methods:// MARK: - UITableViewDelegate func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { sampleJournalEntryData.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...