Editing the list
Adding the ability to edit the list of items by either deleting them or rearranging them is easy as well. All we need to do is implement few more methods to let our ItemTableViewController
know that we want to delete certain rows or rearrange them and write code to update our model representing the list, which is our array of items.
First, let's implement deleting. To turn on deleting, we need to perform the following steps:
- Implement the
tableView(_:canEditRowAt:)
method. In this method, we need to return true and it will allow deleting of all rows and hence all Shopping List Items. Setting this totrue
will allow users to swipe the cell to the left to reveal theDelete
button, or it can be swiped all the way to the left to trigger a delete:
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true }
- Implement
tableView(_:commit:forRowAt:)
. In this method, we need to delete the row from the table and also remove the item...