-1

In UITableViewCell's contentView, I have put a UIView and set its layer cornerRadius to half of its height to make it a circle.And set its color to red. When I run it and press the UITableViewCell,the red circle becomes transparent.

Before press the cell.

enter image description here

After press the cell.

enter image description here

Where goes wrong,I think it is something to do with cornerRadius.Can anyone help me?

tounaobun
  • 14,570
  • 9
  • 53
  • 75

2 Answers2

1

As I have answered here :

UITableViewCell changes the background color of all sub views when cell is selected or highlighted.

I have adapted my previous answer for your question

You have three options :

  1. If you don't want any selection style

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  2. Subclass UITableViewCell and overriding Tableview cell's setSelected:animated and/or setHighlighted:animated like Leo answered

  3. Add the circle as a layer

    CALayer* layer = [CALayer layer];
    layer.frame = CGRectMake(10, 10, 30, 30);
    layer.backgroundColor = [UIColor redColor].CGColor;
    layer.cornerRadius = 20/2.0f;
    [cell.contentView.layer  addSublayer:layer];
    
Community
  • 1
  • 1
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
0

Because UITableviewCell change the view background color when selected.

You need to subclass the UITableviewCell,and change the backgroundcolor back

Screenshot

Code

class CustomCell:UITableViewCell{
override func setHighlighted(highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)
    let view = self.viewWithTag(10) //The tag of this view is 10
    view?.backgroundColor = UIColor.redColor()
}
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    let view = self.viewWithTag(10)
    view?.backgroundColor = UIColor.redColor()
}
} 
Leo
  • 24,596
  • 11
  • 71
  • 92