Changing background color of selected cell?

后端 未结 30 2964
太阳男子
太阳男子 2020-11-29 00:22

Does anyone know how to change the background color of a cell using UITableViewCell, for each selected cell? I created this UITableViewCell inside the code for TableView.

30条回答
  •  醉话见心
    2020-11-29 00:57

    For iOS7+ and if you are using Interface Builder then subclass your cell and implement:

    Objective-C

    - (void)awakeFromNib {
        [super awakeFromNib];
        // Default Select background
        UIView *v = [[UIView alloc] init];
        v.backgroundColor = [UIColor redColor];
        self.selectedBackgroundView = v;
    }
    

    Swift 2.2

    override func awakeFromNib() {
        super.awakeFromNib()
        // Default Select background
        self.selectedBackgroundView = { view in
            view.backgroundColor = .redColor()
            return view
        }(UIView())
    }
    

提交回复
热议问题