47

Delete Rows from Table View iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/E3eeYni
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.

Inside a Table View the rows can be manipulated by user actions. In this tutorial we will delete a row from a Table View using the "swipe-to-delete" gesture. The row will be deleted in the data model(an array) and also inside the Table View itself. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

JjYZraQ.png!web

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

YniEZzU.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.

i2uQvye.png!web

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

zuqIv2Z.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.

ZZVJn2i.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.

7NFFbeM.png!web

Next, we need to add the functionality to delete a row from the Table View with the tableView:commitEditingStyle:forRowAtIndexPath: method

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCell.EditingStyle.delete {
        numbers.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
    }
}

When a user slides horizontally across a row the editing style of the Tabel View Cell is set to delete. When the delete button is pressed, the item is deleted in the array and also the row is deleted in the Table View.  Build and run the project and swipe-to-delete a row from the Table View.

R3eI7bq.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK