Dmitry Dubinin Dmitry Dubinin - 5 months ago 21
Swift Question

The circle uiview is hidden when the cell is selected

The circle uiview is hidden when the cell is selected. How to fix it?
bringSubviewToFront not helping.

screenshot

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.selectedBackgroundView?.bringSubviewToFront(statusView)
}


Please help!

Answer

set your view's backgroundColor by override func setHighlighted(highlighted: Bool, animated: Bool)and func setSelected(selected: Bool, animated: Bool)like this:

class Cell: UITableViewCell {
var redView :UIView!
var yellowView :UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    redView = UIView()
    redView.frame = CGRect(x: 0, y: 10, width: 100, height: 20)
    self.contentView.addSubview(redView)

    yellowView = UIView()
    yellowView.frame = CGRect(x: 200, y: 10, width: 100, height: 20)
    self.contentView.addSubview(yellowView)

    redView.backgroundColor = UIColor.redColor()
    yellowView.backgroundColor = UIColor.yellowColor()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    redView.backgroundColor = UIColor.redColor()
    yellowView.backgroundColor = UIColor.yellowColor()
}
override func setHighlighted(highlighted: Bool, animated: Bool) {
    super.setHighlighted(highlighted, animated: animated)
    redView.backgroundColor = UIColor.redColor()
    yellowView.backgroundColor = UIColor.yellowColor()
}
}