首页 文章

选择单元格时,UITableView所有组件都会更改背景颜色

提问于
浏览
0

我正在定制 tableview 的单元格 . 单元格包括2个按钮(一个是红色,另一个是蓝色) . 但是当我选择单元格时,2个按钮将背景颜色更改为突出显示的单元格颜色 . 我希望他们在突出显示单元格时保持颜色 . 我的解决方案是捕获 tableview 的突出显示的委托并更改按钮的背景 .

任何人都能给我另一种解决方案吗?

谢谢

1 回答

  • 1

    您必须在 UITableViewCell 子类中覆盖 setHighlightedsetSelected

    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
    }
    

相关问题