The Product College early application deadline is Oct 31 - Apply Now

Now that we can add new notes and modify existing notes, let's make it so users can delete notes.

We would like our users to be able to swipe right on a note and be presented with a delete option:

Luckily, table views make this incredibly easy.

Add the following method to the ListNotesTableViewController class:

// 1
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    // 2
    if editingStyle == .delete {
        // 3
        notes.remove(at: indexPath.row)
    }
}
  1. By implementing tableView(_:commitEditingStyle:forRowAt indexPath:), we enable the table view to have additional editing modes, one of which is that the cells display the delete option when a user swipes right. The other mode is an insert mode, but that won't appear without additional configuration.

  2. We check to see if the editingStyle is the .delete one - there's also an .insert one. We wouldn't want to accidentally delete a user's notes when they intended to insert a new one!

  3. We are removing the appropriate note from the notes array, using the row property of the passed in indexPath.

And that's all we have to do to delete a note! =]

Running the App!

You should now be able to add, modify, and delete notes!

In the next tutorial we will start working on persisting data between app launches!

On this page, you should have:

  1. Implemented tableView(_:commitEditingStyle:forRowAtIndexPath:) in the ListNotesTableViewController so that users can delete notes by swiping right on them.
  2. Run and tested your app!

Feedback

If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations across the USA and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more