Ios 选择行并更改按钮颜色

Ios 选择行并更改按钮颜色,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,在我的tableview中,单元格是自定义单元格,我在其中有按钮和标签。 标签和按钮背景颜色为绿色 当我点击特定的行时,按钮的颜色改变,标签的背景色保持原样。在这里,我使用以下代码通过图层设置标签背景颜色 cell.lblCountMsg.backgroundColor = [UIColor clearColor]; cell.lblCountMsg.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 b

在我的tableview中,单元格是自定义单元格,我在其中有按钮和标签。 标签和按钮背景颜色为绿色

当我点击特定的行时,按钮的颜色改变,标签的背景色保持原样。在这里,我使用以下代码通过图层设置标签背景颜色

cell.lblCountMsg.backgroundColor = [UIColor clearColor];
cell.lblCountMsg.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0].CGColor;
但是,同样的代码不适用于Button。我也试过用layer

cell.btnTeting.backgroundColor = [UIColor clearColor];
cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
但没有设置绿色,也改变了选择颜色的颜色

看,我的屏幕截图是绿色的。 请帮我找到解决办法。
提前感谢。

您应该实现如下功能:

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Here your custom cell's class instead of UITableViewCell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.btn.backgroundColor = [UIColor redColor];

}

如果创建了UITableViewCell子类,请尝试重写

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
方法。在该方法中,可以在高亮显示单元时更改单元中的视图

例如:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
     self.btnTeting.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0]; 
}
已更新

@interface SuggestedGroupCell ()
@property (strong, nonatomic) IBOutlet UIButton *btnSubScribe;
@end

@implementation SuggestedGroupCell
  - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
      self.btnSubScribe.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0]; 
  }
@end

如果要更改按钮的背景色,请删除此行,然后重试:

cell.btnTeting.layer.backgroundColor = [UIColor colorWithRed:110/255.0 green:189/255.0 blue:82/255.0 alpha:1].CGColor;
但如果要更改按钮标题颜色,可以编写如下代码:

[cell.btnMorePupils setTitleColor:[UIColor redColor] forState:UIControlStateNormal]


此处uicontrol状态您可以根据您的要求选择uicontrol状态正常、uicontrol状态选择等。

不完全了解问题。是否要将绿色设置为行上按钮的背景色单击??我已经设置了绿色按钮。但当我选择“未加工”时,颜色就变了。@lionar您可以更改标签而不能更改按钮吗?是吗?@Lion是的,我可以更改标签背景颜色。您更改UIButton背景的代码似乎是正确的。这意味着要么是设置了错误的按钮,要么是以后再次设置了背景(并删除了以前的更改),要么是使用了无法通过UI看到的颜色(例如,与突出显示相同的颜色,因此会混淆)。您是否尝试过使用断点来确保代码只在正确的单元格中的正确按钮上以正确的颜色调用一次?如何设置indexPath…=>-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{SuggestedGroupCell=(SuggestedGroupCell)[tblGroupList cellForRowAtIndexPath:indexPath];cell.btnSubScribe.backgroundColor=[UIColor COLOR with RED:110/255.0 green:189/255.0 blue:82/255.0 alpha:1.0];}您需要在SuggestedGroupCell类中添加该方法。btn测试在单元格中。请使用
DIDSELECROW
CellForRow
编辑您的问题。Amd告诉我确切的问题是什么?没有为相关按钮颜色选择DID中的任何代码。你的问题真的不够清楚。您想在选择行时更改颜色吗?如果是,则您必须已实现
didselectrow
,当我选择该时间时,我不想更改颜色。我想保持现状。我的按钮背景色。我的按钮背景颜色为绿色。而不是简单地使用此:[cell.btnTeting setBackgroundColor:[UIColor redColor]];不需要设置图层。背景颜色我也尝试这个,。。你创建演示和检查我已经在我的项目中使用它,工作良好。不要设置clearColor,而是直接设置您想要设置的颜色。你检查过那个按钮的插座了吗?您说您使用了自定义类。在您的代码中,您已经在CellForRowatineXpath方法中设置了背景色,现在,由于您想在选择按钮时更改背景色,您必须在DidSelectRowatineXpath方法中为其编写代码。实际上,有两种方法可以做到这一点,1)直接编写[cell.btnting setBackgroundColor:[UIColor redColor]];在didSelectRowAtIndexPath和2中)维护选定的indexpath数组,并在didSelectRowAtIndexPath中将该indexpath添加到该数组中,然后在cellForRowAtIndexPath中放入如下条件:if([arSelectedIndex containsObject:indexpath]){[cell.btnting setBackgroundColor:[UIColor redColor]];}else{cell.btnTeting.backgroundColor=[UIColor COLOR WITHRED:110/255.0绿色:189/255.0蓝色:82/255.0 alpha:1];}