On iPad, in a UISplitViewController, I have a UITableViewController in the right section showing the details of what is selected on the left. So when I deselect everything on the left, consequently setting the detail object to nil in the detail view, I would like the table to clear and to display a message to invite the user to select an element.
That's why I have the following in my detail UITableViewController subclass:
var reference: Reference? {
didSet {
self.refreshInterface()
}
}
func refreshInterface() {
if let titleLabel = self.titleLabel {
if let ref = reference {
titleLabel.text = ref.projectName
} else {
titleLabel.text = NSLocalizedString("Please select a reference", comment: "")
}
}
tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
if(self.reference != nil) {
return 5
} else {
return 0
}
}
And I checked with the debugger that after I set the reference to nil, numberOfSections is called and returns 0. But then for some reason, all the UI of the table isn't cleared. I still have the same rows and sections as when I had a reference selected. Am I missing something? Is this a potential bug in Cocoa Touch?
I figured it out. The problem was not in my detail view controller, but in the master one. I didn't take into account that when I show a detail view controller when selecting an item in the main one, the split view controller creates another instance of the detail view controller each time. And since I was setting the detail object on the old instance, the old instance was updated but another one was already displayed. All I had to do to fix my issue was to make sure that I always set my detail object on the current instance of the detail view controller.
I hope this is useful to somebody.