如何根据用户选择更改单元格内的按钮颜色?

How to change the button colour inside the cell depend upon user selection?

这是我的按钮,当用户select按钮时,它会显示另一种颜色。

 UIButton *btnClk=[[UIButton alloc]initWithFrame:CGRectMake(0, 50, 50, 50)];
    [btnClk setTitle:@"Click" forState:UIControlStateNormal];
    btnClk.backgroundColor=[UIColor blackColor];
    [btnClk addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btnClk];

方法:

   -(void)btnClicked:(UIButton *)click
{

         NSIndexPath *ind1=[NSIndexPath indexPathWithIndex:click.tag];
      NSIndexPath *ind2=[NSIndexPath indexPathWithIndex:click.tag];
    if(ind1==ind2)
    {
       btnClk.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
    }
    else
    {
        btnClk.backgroundColor=[UIColor blackColor];
    }
}

按钮声明是全局的。

您的 ind1ind2 将始终相同。你想比较什么?

我猜你想更改密码

btnClk.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];

click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];

如果按钮声明是全局的,那么你也可以这样做..我希望它能有所帮助..

-(void)btnClicked:(UIButton *)click
{
 UIButton *button = (UIButton *)click;
 if(!button.selected)
 {
   btnClk.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
    button.selected=YES;
 }
 else
 {
    button.selected=NO;
    btnClk.backgroundColor=[UIColor blackColor];
 }
}