Cong Phu Cong Phu - 1 month ago 4
iOS Question

UITableView all components are changed background color when selecting cell

I'm customizing the cell of tableview. The cell included 2 buttons(one is red and another is blue). But when I select the cell, 2 buttons were changed background color to the highlighted cell color. I want they keep their color when the cell is highlighted. My solution is to capture the highlighted delegate of

tableview
and change the background of the buttons.

Can any one give me another solution?

Thanks

Answer

You have to override setHighlighted and setSelected inside your UITableViewCell subclass

Objective C

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    UIColor *someViewColor = self.someView.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.someView.backgroundColor = someViewColor;
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIColor *someViewColor = self.someView.backgroundColor;
    [super setSelected:selected animated:animated];
    self.someView.backgroundColor = someViewColor;
}

Swift

override func setHighlighted(highlighted: Bool, animated: Bool) {

    let someViewColor = someView.backgroundColor        
    super.setHighlighted(highlighted, animated: animated)
    someView.backgroundColor = someViewColor
}

override func setSelected(selected: Bool, animated: Bool) {

    let someViewColor = someView.backgroundColor        
    super.setSelected(selected, animated: animated)
    someView.backgroundColor = someViewColor
}
Comments