Swift / Enable Editing Mode in View Controller

mostworld77 picture mostworld77 · Jun 7, 2015 · Viewed 11k times · Source

Because of UI elements, I created View Controller with a TableView inside. But I can't enable editing mode. I tried several methods without Solution. But with using TableView Controller there are no problems.

What I tried:

override func viewDidLoad() {
  self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func setEditing(editing: Bool, animated: Bool) {
 super.setEditing(editing, animated: animated)
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            // Action to delete data
        }
    }

Where can be the problem?

Answer

rmaddy picture rmaddy · Jun 7, 2015

You forgot to put the table view in editing mode:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    self.tableView.setEditing(editing, animated: animated)
}

I'm assuming you have a tableView property. Adjust as needed.

Some other things you may want to do to emulate a UITableViewController:

  1. In viewWillAppear you should deselect the currently selected row in the table view.
  2. In viewDidAppear you should flash the scrollbars of the table view.