如何更改按钮颜色交替选择按钮?

时间:2015-09-21 12:44:02

标签: ios objective-c uitableview uibutton

这是我正在使用的代码, selectindex是BOOL

new \DateTime->format('Y-m-d H:i:s')

我的问题是当用户选择我的按钮时它正确地改变颜色,当用户尝试选择另一个按钮时,它继续使用之前的bool值。

- >我的要求是当用户点击按钮时颜色必须改变。

- >第二,当用户选择相同的按钮时,颜色必须改变。

- >按钮放在tableview里面每个按钮都有标签,我试图改变使用标签值但是失败了。任何人请帮助我....

第三方Tableview标题部分:

if(!selectindex)
    {
        click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];

        selectindex=YES;
    }
    else
    {
        click.backgroundColor=[UIColor grayColor];

        selectindex=NO;
    }

这是我期待的...... enter image description here

最初选择索引 - >

当用户选择按钮颜色改变时。

现在selectIndex值为; 当用户尝试选择另一个按钮时, selectIndex值继续为真。当用户第二次单击该按钮时,其颜色已更改。   请注意按钮选择应为多个:

6 个答案:

答案 0 :(得分:2)

您应该为字典或班级中的每个单元格保存状态

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

       return [yourArray  count];
}

并在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


    NSMutableDictionary *dictionary = [yourArray objectAtIndex:indexPath.row];

 if(dictionary[@“selectindex”] ==NO )
{

         click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
         NSLog(@"kishore kumar”); 
         [dictionary setValue:YES forKey:@“ selectindex”];

    }else{

        click.backgroundColor=[UIColor grayColor];
        [dictionary setValue:NO forKey:@“selectindex”];

    }

答案 1 :(得分:0)

请详细说明,按照我的理解给出答案。

首先选择一个NSMutableArray *arr_index;

在按钮上单击将当前IndexPath存储到arr_index&重装表

在tableview的cellforRow方法中写下代码

if([arr_index containsObject:indexPath])
{
 // change your button bgColor
}
else
{
// Default Color for button
}

答案 2 :(得分:0)

// chekMarkArray最初包含0.// [checkMarkArray addObject:@(0)];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 //………
    BOOL selectindex = [[chekMarkArray objectAtIndex:indexPath.row] boolValue];
    if (!selectindex) {
        testButton.backgroundColor = [UIColor greenColor];//[UIColor colorFromHexString:@"#ffc400"];
    }else{
        testButton.backgroundColor = [UIColor redColor];
    }

    //………..
return cell;

}

-(IBAction)buttonPressed:(id)sender{
UIButton *button = (UIButton*)sender;
BOOL currentStatus = [[chekMarkArray objectAtIndex:button.tag] boolValue];
//deselect all selection
for (int i=0;i<chekMarkArray.count;i++) {
    [chekMarkArray replaceObjectAtIndex:i withObject:@(0)];
}
//select current button
[chekMarkArray replaceObjectAtIndex:button.tag withObject:@(!currentStatus)];
[self.tableView reloadData];

}

答案 3 :(得分:0)

您可以轻松地按照此方法执行此操作。 每个按钮都有一个名为“selected”的属性。您可以使用它而不是为每个单独的按钮创建BOOL变量。 假设您的按钮名称为“click”,只需检查按钮的状态并按

设置颜色
if(click.selected){
    click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
}
else{
    click.backgroundColor=[UIColor grayColor];
}

由于selected是一个属性,其BOOL值由其内置函数处理,因此您不必将其设置为YES或NO。

编辑:

找到一种更简单的方法。 将选定和未选定按钮的图像作为资源添加到项目中并进行设置。

`[button setBackgroundImage:[UIImage imageNamed:@“selected.png”] forState:UIControlStateSelected];

[button setBackgroundImage:[UIImage imageNamed:@“unselected.png”] forState:UIControlStateNormal]; `

答案 4 :(得分:0)

检查UITreeView它是否具有功能

答案 5 :(得分:0)

最后,我找到了回复感谢你的回复朋友......

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

    if(click.selected==NO)
    {
        click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"];
        click.selected=YES;
    }
    else
    {
         click.backgroundColor=[UIColor grayColor];
        click.selected=NO;
    }

}

选择的默认状态为NO。

@property(nonatomic,getter=isSelected) BOOL selected;                                // default is NO may be used by some subclasses or by application
相关问题