How do I programmatically implement editing of a UITableViewCell?

I have a UITableView added as a subview to a UIViewController and I used the built-in editButtonItem with

self.navigationItem.rightBarButtonItem = editButtonItem

Also, after reading other forum threads, I called these functions:

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

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

When I tap "Edit", it does toggle the red minuses for each cell and it changes to "Done", but I still can't edit the text for the cells. Am I missing something?

Where did you read that setEditing allowed to change the content ? It is only to remove, insert or move cells in the table.

To do what you want, you need to implement the cell editing func, independently of setEditing.

For instance,

  • have a button in cell to edit, or
  • insert such button in a left of rightSwipe on the cell.

Then,

  • if it is a label: overlay a textField over the label you want to edit,
  • if it is a textField, when you setEditingOn, enable the textField for user interaction for the textField
self.textField.enabled = true 
  • insert a done button somewhere (or rely on return)
  • preload with the present label content
  • when text edited, fill the label with it

And, essential,

  • don't forget to update your data source.

Get details here: http://www.apeth.com/iOSBook/ch21.html#_table_view_editing

it is objC, but easy to adapt.

How do I programmatically implement editing of a UITableViewCell?
 
 
Q