Delete Rows from Table View in iOS8 with Swift

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 built for iOS 8 and Xcode 6.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftDeleteTableViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and Remove the View Controller. Drag a Table View Controller from the Object Library to the empty canvas. Go to the Editor Menu and choose Embed in -> Navigation Controller. 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:cellForRowAtIndexPath method

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("numberCell") as UITableViewCell
    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.

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, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.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.

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