UIButton not showing as selected

I am totally stumped.


I have a UIButton in a UITableViewCell. I set the UIButton.isSelected property to true from the UITableViewController. The code to do that is in an @IBAction method that a UIViewController unwinds to that I use to edit the selected row on the table view. The code runs. I even step through it and I see that it runs. But the UIButton doesn't show as selected.


Anyone know what's going on here?

Replies

How exactly are you setting the button's state? What you typically want to do for controls in table view cells, is set their state based on your model. e.g. say you have a model with String and Bool properties. In your tableView:cellForRowAt: implementation, you'd set the cell's text and the button's state based on the model for the particular row.

Which properties particularly would that be that you're talking about. I particularly used the image property.


        buttonCheckBox.setImage(UIImage(named: "solid empty check box 30x30.png"), for: UIControlState.normal)
        buttonCheckBox.setImage(UIImage(named: "grayed empty check box 30x30.png"), for: UIControlState.disabled)
        buttonCheckBox.setImage(UIImage(named: "check box checked red 30x30.png"), for: UIControlState.selected)


I figured out it would work if I set the isEnabled and the isSelected properties to true, then tableViews.reloadRows(), then set those properties to true again. I think the reloadRows was interfering with the UIButton in the table view cell from getting enabled and selected.