58

Delete Multiple Rows from Table View iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/uYrE73J
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

YVJjyaN.png!web

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.

mMbYjqM.png!web

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.

miqe6vI.png!web

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

yuYRNv2.png!web

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 number Cell . We will need this Identifier as a reference into our code.

j22q6vi.png!web

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 Runthe project, The rows of the Table Views are filled with the numbers from the array.

u2AzAfz.png!web

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

iuAjeuQ.png!web

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

nURfyyr.png!web

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

BFn6Vn6.png!web

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  {
            items.append(numbers[indexPath.row])
        }
        // 2
        for item in items {
            if let index = numbers.index(of: item) {
                numbers.remove(at: index)
            }
        }
        // 3
        tableView.beginUpdates()
        tableView.deleteRows(at: selectedRows, with: .automatic)
        tableView.endUpdates()
    }
}
  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 Runand select and delete multiple rows in the table view.

ZbiE3e7.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK