当单元格被选中时,按钮的背景颜色会改变然后恢复 [英] When cell is selected, the backgound color of the button will change and then recorver

查看:23
本文介绍了当单元格被选中时,按钮的背景颜色会改变然后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 UITableViewCell 并且其中有一个按钮,当我将按钮的背景颜色设置为 Default Color(即 [UIColor whiteColor]),那么当单元格被选中时,按钮的背景颜色会发生变化,当单元格的选中高亮消失后会恢复.

I have a Customize UITableViewCell and there is a button within it, when I set the background color of the button to some color other than Default Color(which is [UIColor whiteColor]), then when the cell is selected, the background color of the button will change and recover when the selection highlight of cell is disappear.

这是如何发生的以及如何在选择单元格时保持按钮的背景颜色?

How this happen and what to do to keep button's background color when cell is selected?

推荐答案

我建议检查表格视图单元格的视图层次结构.这很容易完成:在模拟器上运行您的应用程序 -> 单击Debug View Hierarchy"(请参阅​​所附的第一张图片)-> 与检查器一起玩(请参阅所附的第二张图片).

I would recommend inspecting the hierarchy of views you've got for your table view cells. It can be easily done: Run your app on the simulator -> Click "Debug View Hierarchy" (see the 1st image attached) -> Play around with the inspector (see the 2nd image attached).

更新:在挖掘了一段时间后,我得到了以下信息(请参阅下面解释情况的 3 个屏幕截图):

Update: After digging for a while I got the following (please see the 3 screenshots below explaining the situation):

如您所见,有一个内部调用清除名为 _setOpaque:forSubview:

As you can see, there's the internal call that clears the background colors of a cell's subviews named _setOpaque:forSubview:

话虽如此,如果您想确保按钮的背景颜色保持不变,您应该通过以下方式实现单元格的选择机制:

Having that said, if you want to ensure the background color of your button remains the same you should implement the selection mechanism of your cell in the following way:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // here comes your custom color (it's assumed that myButton is a binded IBOutlet
    self.myButton.backgroundColor = [UIColor greenColor]; // or whatever else
}

这是不友好的 UIKit.希望这对您有所帮助.

It was unkind of UIKit. Hope this will help you.

这篇关于当单元格被选中时,按钮的背景颜色会改变然后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆