Delete Multiple Rows from Table View iOS Tutorial

The Table View Controller has an editing mode,, when enabled multiple rows can be selected and deleted.. In this tutorial we will fill the Table View with some data and delete multiple rows at once. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use iOS12DeleteMultipleRowsTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and Remove the View Controller. Drag a Navigation Controller from the Object Library to the empty canvas, this will also contain a table view controller. When the initial View Controller is deleted there isn't a starting point defined. Select the Navigation Controller and go to the Attribute Inspector. In the View Controller Section  select the "Is Initial View Controller" checkbox.


Double-click the Title Bar in the Table View Controller and insert the title Numbers. The storyboard should look like this.

We will use a basic layout of our cells, so select the Table View Cell and go to the Attributes Inspector. Change the style to Basic and insert as Identifier numberCell. We will need this Identifier as a reference into our code.

Go to the ViewController.swift file. Since we have changed the default View Controller into an Table View Controller we also need to update the class declaration line in

class ViewController: UITableViewController {

Next, we create an array containing the numbers. 

var numbers = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"]

We also need to determine the number of rows in our Table View. Add the tableView(_:numberOfRowsInSection:) method

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return numbers.count

We will need to fill our cells with data. Add the tableView(_:cellForRowAt:) method

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell", for: indexPath)
    cell.textLabel?.text = numbers[indexPath.row]
    return cell

Build and Run the project, The rows of the Table Views are filled with the numbers from the array.

To delete multiple rows in a Table View we have to make sure our Table View is in editing mode. Go to the Main.Storyboard and add a Bar Button Item to the right of the Navigation Bar of the Table View Controller. Select the Bar button item and in the Attributes inspector change the title to Edit. Add another Bar Button item and change the tile to Delete

Open The Assistant Editor and make sure the TableViewController.swift file is visible. Ctrl And Drag from the Edit Bar Button Item to the TableViewController class and create the following Action

Ctrl And Drag from the Delete Bar Button Item to the TableViewController class and create the following Action

Implement the startEditing(_:) method

@IBAction func startEditing(_ sender: Any) {
    isEditing = !isEditing

The Table View Controller has an editing property to set the Table View into editing mode.  While in editing mode multiple table view rows can be selected. Pressing the Edit button will toggle the editing mode. Implement the deleteRows(_:) method

@IBAction func deleteRows(_ sender: Any) {
    if let selectedRows = tableView.indexPathsForSelectedRows {
        // 1
        var items = [String]()
        for indexPath in selectedRows  {
        // 2
        for item in items {
            if let index = numbers.index(of: item) {
                numbers.remove(at: index)
        // 3
        tableView.deleteRows(at: selectedRows, with: .automatic)
  1. The selected rows are added to a temporary array

  2. the index of the items of the temporary array will be used to remove the items of the numbers array

  3. The selected rows will be deleted from the table view

Build and Run and select and delete multiple rows in the table view.

You can download the source code of the iOS12DeleteMultipleRowsTutorial at the ioscreator repository on Github.