UIView backgroundColor disappears when UITableViewCell is selected

前端 未结 17 817
谎友^
谎友^ 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:32

    Swift 4

    In your UITableViewCell class:

    override func setSelected(_ selected: Bool, animated: Bool) {
        myView.backgroundColor = UIColor.blue
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        myView.backgroundColor = UIColor.blue
    }
    
    0 讨论(0)
  • 2020-11-30 18:33
    - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
    {
        UIColor *backgroundColor = self.channelImageView.backgroundColor;
        [super setHighlighted:highlighted animated:animated];
        self.channelImageView.backgroundColor = backgroundColor;
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        UIColor *backgroundColor = self.channelImageView.backgroundColor;
        [super setSelected:selected animated:animated];
        self.channelImageView.backgroundColor = backgroundColor;
    }
    
    0 讨论(0)
  • 2020-11-30 18:34

    Just spent some time on this weird issue. I did not want to set UITableViewCellSelectionStyleNone style to preserve nice animation when my row was selected. But none of suggested ideas worked for me - I was trying to override setSelected and setHighlighted and set my subview backgroundColor there - it was keeping resetting by iOS and still blinking (new color -> old color). For me the fix was quite simple. When my row is selected another view controller is pushed, user chooses some option on that screen and delegate is called where I change the color based on user selection. In this delegate I just do [cell setSelected:NO animated:NO] for my cell. (I have static UITableViewController and have outlets to cells). You could probably deselect cell in didSelect method but in my case I'm using segues.

    0 讨论(0)
  • 2020-11-30 18:37

    Brooks has a great explanation for why this happens, but I think I have a better solution.

    In your subview, override setBackgroundColor: to what ever color you want. The setter will still be called, but only your color specified will be enforced.

    - (void)setBackgroundColor:(UIColor *)backgroundColor {
        [super setBackgroundColor:[UIColor whiteColor]];
    }
    
    0 讨论(0)
  • 2020-11-30 18:39

    Related to @Brooks's answer, this is what I did to make it work in Swift and iOS8/iOS9.

    • Override the setSelected and setHighlighted
    • Don't call super
    • Clear out the contentView.backgroundColor, because it does not have to span over the whole width of the cell (i.e. accessories).
    • Use the backgroundColor of the cell itself, and set it accordingly.

      class AwesomeTableViewCell: UITableViewCell {
      
          private struct Constants {
      
              static var highlightedColor = UIColor.greenColor()
              static var selectedColor = UIColor.redColor()
      
              static let animationTime = NSTimeInterval(0.2)
          }
      
          override func awakeFromNib() {
              super.awakeFromNib()
      
              contentView.backgroundColor = UIColor.clearColor()
              backgroundColor = AppContext.sharedInstance.theme.colors.background
          }
      
          override func setHighlighted(highlighted: Bool, animated: Bool) {
              if animated {
                  UIView.animateWithDuration(Constants.animationTime, animations: { () -> Void in
                      self.setHighlighted(highlighted)
                  })
              } else {
                  self.setHighlighted(highlighted)
              }
          }
      
          override func setSelected(selected: Bool, animated: Bool) {
      
              if animated {
                  UIView.animateWithDuration(Constants.animationTime, animations: { () -> Void in
                      self.setSelected(selected)
                  })
              } else {
                  self.setSelected(selected)
              }
          }
      
          private func setHighlighted(highlighted: Bool) {
      
              backgroundColor = highlighted ? Constants.highlightedColor : UIColor.whiteColor()
          }
      
          private func setSelected(selected: Bool) {
      
              backgroundColor = selected ? Constants.selectedColor : UIColor.whiteColor()
          }
      }
      
    0 讨论(0)
提交回复
热议问题