UIView backgroundColor disappears when UITableViewCell is selected

前端 未结 17 1044
谎友^
谎友^ 2020-11-30 17:49

I have a simple tableViewCell build in interface builder. It contains a UIView which contains an image. Now, when I select the cell, the default blue selection background is

17条回答
  •  情歌与酒
    2020-11-30 18:28

    When your UITableViewCell is selected, there are two states you should pay attention to: Highlighted and Selected.

    So, for scenarios that you have a custom cell class which is subclass of UITableViewCell, you can easily override these two methods to avoid this situation(Swift):

    class MyCell: UITableViewCell {
    
        @IBOutlet var myView: UIView!
    
        override func setHighlighted(highlighted: Bool, animated: Bool) {
            let myViewBackgroundColor = myView.backgroundColor
            super.setHighlighted(highlighted, animated: animated)
            myView.backgroundColor = myViewBackgroundColor
        }
    
        override func setSelected(selected: Bool, animated: Bool) {
            let myViewBackgroundColor = myView.backgroundColor
            super.setSelected(selected, animated: animated)
            myView.backgroundColor = myViewBackgroundColor
        }
    
    }
    

提交回复
热议问题