Set table view into editing mode

Here is a solution for Swift 4.2:

override func viewDidLoad() {
  super.viewDidLoad()

  // Use the edit button provided by the view controller.
  navigationItem.rightBarButtonItem = editButtonItem
}

override func setEditing(_ editing: Bool, animated: Bool) {
    // Takes care of toggling the button's title.
    super.setEditing(editing, animated: true)

    // Toggle table view editing.
    tableView.setEditing(editing, animated: true)
}

The view controller's setEditing is called by default when the editButtonItem is pressed. By default, pressing the button toggles its title between "Edit" and "Done", so calling super.setEditing takes care of that for us, and we use the tableView's setEditing method to toggle the editing state of the table view.

Sources:

  • https://developer.apple.com/documentation/uikit/uiviewcontroller/1621471-editbuttonitem
  • https://developer.apple.com/documentation/uikit/uiviewcontroller/1621378-setediting
  • https://developer.apple.com/documentation/uikit/uitableview/1614876-setediting

Swift 3 & 4 answer that IMHO is better than other answers:

override func viewDidLoad() {
    super.viewDidLoad()

    let editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(toggleEditing)) // create a bat button
    navigationItem.rightBarButtonItem = editButton // assign button
}

@objc private func toggleEditing() {
    listTableView.setEditing(!listTableView.isEditing, animated: true) // Set opposite value of current editing status
    navigationItem.rightBarButtonItem?.title = listTableView.isEditing ? "Done" : "Edit" // Set title depending on the editing status
}

Why do I think it's better:

  1. Fewer code lines.
  2. Bar button is initialized once but not every time you press the button.

Create rightBarButtonItem as below with an action.

In viewDidLoad() :

let rightButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showEditing:"))
    self.navigationItem.rightBarButtonItem = rightButton

and then make a function like,

 func showEditing(sender: UIBarButtonItem)
 {
    if(self.tableView.isEditing == true)
    {
        self.tableView.isEditing = false
        self.navigationItem.rightBarButtonItem?.title = "Done"   
    }
    else
    {
        self.tableView.isEditing = true
        self.navigationItem.rightBarButtonItem?.title = "Edit" 
    }
}

Make sure, : is appended to function name in Selector of action in viewDidLoad
Hope it helps!


Call this method on button click.

tableView.setEditing(true, animated: true)

Or if you want it to work like a toggle use

tableView.setEditing(!tableView.editing, animated: true)

I assume you have a button, which calls editButtonPressed on press. So implementation of this method could look like this.

override func viewDidLoad(){
 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
}

func editButtonPressed(){
  tableView.setEditing(!tableView.editing, animated: true) 
  if tableView.editing == true{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }else{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }
} 

This also changes title of the bar button.