Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isHighlighted and isSelected didSet only called for UICollectionViewCell not UITableViewCell

I want to apply some style changes to a custom table view cell on highlight/select so am overriding isHighlighted and isSelected to achieve this. It works for my custom collection view cells but not when I tap on the custom table view cells.

I set up the exact same scenario for a table view and collection view and implemented the following on the custom cells:

override var isHighlighted: Bool {
   didSet {
     //called when I tap for CustomCollectionViewCell not for CustomTableViewCell
   }
}

override var isSelected: Bool {
  didSet {
     //called when I tap for CustomCollectionViewCell not for CustomTableViewCell
  }
}

What am I missing here? Why doesn't the table view cell did set get called when it's tapped? This happens for any table view I try with regardless of the content of the custom cell.

like image 468
jeh Avatar asked Jan 30 '18 01:01

jeh


1 Answers

The other answer did not work for me. I reckon the reason is that UITableViewCell.isSelected setter is never called when the containing UITableView handles selection, that state is passed via func setSelected(_ selected: Bool, animated: Bool) instead. This means that overriding this function in your UITableViewCell subclass instead of the setter works:

override func setSelected(_ selected: Bool, animated: Bool) {
  // implementation that was meant to be in `isSelected` `didSet`
}
like image 94
Max Desiatov Avatar answered Nov 06 '22 20:11

Max Desiatov