1

UITableViewCell apparently calls setHighlighted: and setSelected: on eligible subviews (UIImageView, UILabel, etc) when it gets selected. Say I have a custom UIView subclass that I have in a cell, and I want it to get the same calls. I've implemented setHighlighted: and setSelected: on it, but no call comes. Is there some interface I have to declare conformance to? Or am I going to have to use a custom subclass of UITableViewCell, override setSelected:, and walk through my subviews myself?

It seems weird that they wouldn't just walk the subviews and ask each one if it responds to setSelected: and/or setHighlighted:.

Community
  • 1
  • 1
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145

2 Answers2

0

Create subclass of UITableViewCell (if you use storyboards, add your custom UIView and add @IBOutlet). Override setSelected in your subclass of UITableViewCell:

class CustomTableViewCell: UITableViewCell {

     @IBOutlet weak var customView: CustomView!

     //Other methods and properties


         override func setSelected(selected: Bool, animated: Bool) {
             super.setSelected(selected, animated: animated)
             customView.setSelected(selected)
         }
}
zuziaka
  • 575
  • 3
  • 10
  • I'm hoping to avoid having to make the cell talk explicitly to its special subview(s). My goal is to get the cell to treat my custom view the same way it does any other selectable subview. – Tom Hamming Nov 20 '15 at 18:10
  • OK, I think your only chance is to set explicitly: `cell.selectionStyle = .Gray`. Or any other selectionStyle. – zuziaka Nov 20 '15 at 18:11
0

Looking at the Source Code helped. After some trial and error you need to include the following pieces:

@property (assign,getter=isHighlighted,nonatomic) BOOL highlighted;

- (void)setHighlighted:(BOOL)highlighted ;
- (BOOL)isHighlighted;

Once added, my custom UIView received the cell highlighted events.

Good Luck!

Community
  • 1
  • 1
LEO
  • 2,572
  • 1
  • 26
  • 31