UITableViewController delete table cell example in Swift

In this video I show you how to implement a feature that allows user to delete UITableViewCell.

Source code:

// MyTableViewController.swift
// MyTableCellDeleteExample

import UIKit

class MyTableViewController: UITableViewController {

var dataHolder:Array = [“One”,”Two”,”Three”]

override func viewDidLoad() {

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem()


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: – Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return dataHolder.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier(“myCell”, forIndexPath: indexPath) as! UITableViewCell

// Configure the cell…

cell.textLabel?.text = dataHolder[indexPath.row]

return cell

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.

var returnValue:Bool = false
let valueToDelete:String = “Three”

if(dataHolder[indexPath.row] == valueToDelete)
returnValue = true

return returnValue

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

if editingStyle == .Delete {
// Delete the row from the data source

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view