当单元格被选中时,button的背景颜色会改变,然后录制

我有一个自定义的UITableViewCell,并且里面有一个button,当我将button的背景颜色设置为除Default Color[UIColor whiteColor] )以外的Default Color ),那么[UIColor whiteColor]该单元格时,背景颜色当单元格的select亮点消失时,button将会改变并恢复。

这是怎么发生的,以及如何select单元格时保持button的背景颜色?

我会build议检查你的表视图单元格的视图的层次结构。 它可以很容易地完成:在模拟器上运行你的应用程序 – >点击“debugging视图层次结构”(见附图1) – >与检查员一起玩(见附图2)。 在这里输入图像说明

在这里输入图像说明

更新:挖了一段时间后,我得到了以下(请参阅下面的3个截图解释情况): 在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

正如你所看到的,内部调用清除了名为_setOpaque:forSubview:的单元子视图的背景颜色_setOpaque:forSubview:

话虽如此,如果你想确保你的button的背景颜色保持不变,你应该以下列方式实现你的单元格的select机制:

 - (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。 希望这会帮助你。

我不知道你的意思是恢复。 但是,当使用- (void)layoutSubviews{}将单元格添加到单元格时,您希望添加目标操作。 在这种情况下,自我将是自定义表格视图单元格。 您需要在自定义tableViewCell类文件的.m中使用此代码。

  - (void) layoutSubviews { [self.button addTarget:self forAction:@selector(buttonPressedDown) forControlEvents:UIControlEventTouchedUpInside] } //Cell goes to this method once button has been pressed. - (void) buttonPressedDown{ self.backgroundColor = [UIColor redColor];//Replace withwhatever color you want. } 

你可以编辑buttonPressedDown条件, if([self.backgroundColor isEqualTo:[UIColor redColor]])如何处理button按下,如果背景颜色已经改变。 希望这有助于给你更多的信息,当你的意思是恢复单元格亮点消失。 不知道你的意思。