How can I delete a row from UITableView?

How can I delete a row from UITableView?

 

In iOS app, user normally swipes across a row to initiate the delete button. Recalled that we have adopted the UITableViewDataSource protocol, if you refer to the API doc, there is a method named tableView:commitEditingStyle:forRowAtIndexPath. When user swipes across a row, the table view will check to see if the method has been implemented. If the method is found, the table view will automatically show the “Delete” button.

Simply add the following code to your table view app and run your app:

 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

}

Screen Shot 2017 08 07 at 6.50.36 PM

Even the method is empty and doesn’t perform anything, you’ll see the “Delete” button when you swipe across a row.

The next thing is to add code to the method and remove the actual table data. Like other table view methods, it passes the indexPath as parameter that tells you the row number for the deletion. So you can make use of this information and remove the corresponding element from the data array.

Therefore, once the underlying data is removed, we need to invoke “reloadData” method to request the table View to refresh. Here is the updated code:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

// Remove the row from data model

// Request table view to reload

}

 

 

Screen Shot 2017 08 07 at 6.51.10 PM

import UIKit

 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var samples: [String] = [“Sample-1”, “Sample-2”, “Sample-3”, “Sample-4”, “Sample-5”]

let cellReuseIdentifier = “cell”

@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self
}

// number of rows in table view

Screen Shot 2017 08 07 at 6.50.57 PM

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.samples.count
}

 

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {Screen Shot 2017 08 07 at 6.50.57 PM

// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

// set the text from the data model
cell.textLabel?.text = self.samples[indexPath.row]

return cell
}

 

// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(“You tapped cell number \(indexPath.row).”)
}

 

// this method handles row deletion
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == .delete {

// remove the item from the data model
self.samples.remove(at: indexPath.row)

// delete the table view row
tableView.deleteRows(at: [indexPath], with: .fade)

} else if editingStyle == .insert {
// Not used in our example, but if you were adding a new row, this is where you would do it.
}
}

}

 

Simulator Screen Shot 07 Aug 2017 6.50.05 PM


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top