After playing around with this, here are the things to know:
There are separate setEditing routines for the controller and for the tableView. Make sure to use the one for the controller.
Also, it needs a delay as noted above. For this, I used Matt’s delay function.
As an added bonus, one can disable the Edit button when there are no items in your list. The button becomes enabled again when an item is added. Code is in Swift5.
var someArray: [SomeStruct] = [] {
didSet {
if let btn = navigationItem.rightBarButtonItem {
if someArray.count > 0 {
btn.isEnabled = true
} else {
btn.isEnabled = false // Needs to respond immediately - don't put in delay
closeDownEditMode()
}
}
}
}
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
func closeDownEditMode() {
delay(0.1) { [weak self] in
self?.setEditing(false, animated: true)
}
}