0

I have a few buttons in a table view cell row

enter image description here.

WhenI click the button, I want the button change the colour to yellow colour without affect another row of the button. enter image description here. Example when I click the row 0 button then it will change to yellow colour, if I click on row 1 button will change to yellow as well but the row 0 and other row button will change back/stay at original colour.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
NSX
  • 25
  • 3
  • You'll have to store the "selection" state of the button for each row/section in a kind of data model, and then in `tableView:cellForRowAt:` check the state and draw the button accordingly. To update the state, change the model and call `reloadCells...`. – Andreas Oetjen Mar 09 '18 at 09:01
  • You have to manage array for selected buttons – Jaydeep Vora Mar 09 '18 at 09:01

1 Answers1

0

In your case, you can override isHighlighted or isSelected property.

An example (Swift 3) as following.

class MyCustomCell: UITableViewCell {

        @IBOutlet var weak aButton: UIButton!
        @IBOutlet var weak aLabel: UILabel!

        override var isSelected: Bool {
        didSet {
            // custom selected behavior
            aButton.isSelected = isSelected
            aLabel.textColor = (isSelected ? UIColor.red : UIColor.blue)
           }
        }

        override var isHighlighted: Bool {
            didSet {
               // custom highlighted behavior
               aButton.isSelected = isHighlighted
               aLabel.textColor = (isHighlighted ? UIColor.red : UIColor.blue)
            }
        }
}
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • Is this will override `isHighlighted` or `isSelected` property of UITableViewCell instead of `UIButton` right ? – byJeevan Mar 09 '18 at 10:16
  • `didSet` is property observation in `Swift`. You can refer [this post.](https://stackoverflow.com/a/24006282/419348). – AechoLiu Mar 12 '18 at 01:50