2015-09-21 159 views
0

这是我使用的代码,selectindex是BOOL如何更改按钮颜色替代选择按钮?

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

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

     selectindex=NO; 
    } 

我的问题是,当用户选择我的按钮它改变颜色正常,当用户试图选择另一个按钮其继续与以前的bool值。

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

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

- >按钮放在里面的tableview每个按钮都标记,我尝试使用标签值,但failed.Any一个请帮我改....

第三方泰伯维页眉部分:

- (UIView *)mTableView:(TQMultistageTableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    UIView *viewHeader=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.frame.size.height)]; 

    UIButton *btnClick; 
lblHead=[[UILabel alloc]initWithFrame:CGRectMake(50,3,150, 50)]; 
     btnClick=[[UIButton alloc]initWithFrame:CGRectMake(0,10,30, 30)]; 

    [btnClick addTarget:self action:@selector(touchup:) forControlEvents:UIControlEventTouchUpInside]; 
    btnClick.clipsToBounds = YES; 
    btnClick.layer.masksToBounds=YES; 
    btnClick.layer.cornerRadius=btnClick.frame.size.height/2; 
    [btnClick setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal]; 
    btnClick.backgroundColor=[UIColor grayColor]; 
    btnClick.tag=section; 
    [viewHeader addSubview:btnClick]; 
    return viewHeader; 
} 

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

选择指数initially->没有

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

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

+0

你在哪种方法中编写了这段代码? 'didSelectRowAtIndexPath方法'? – Jamil

+0

@ jamil65able我已根据您的要求更新mycode ...... –

+0

小问题,如果您设置了按钮图像?设置背景颜色有什么意义? –

回答

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 
0

请详细描述,我给出答案,根据我的理解。

先取一个NSMutableArray *arr_index;

在点击按钮店目前IndexPath到arr_index &重装表

在下面的代码

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

选择应该是按钮点击兄弟... –

+0

您可以按照您的意愿处理cellforrow方法中的按钮操作事件并更改bgcolor或按钮bg图像。这是你的兄弟工作..? –

+0

没有这不是我的要求兄弟,我的应用程序选择应该是多个,如果相同的按钮点击的颜色必须改变为以前的颜色.. –

2

的tableview写的cellforRow方法,您应保存状态为每个小区在字典或在你的班级

-(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”]; 

    } 
+0

该选择应该是按钮不是单元格... –

0

// chekMarkArray最初含有0.//[chekMarkArray 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]; 

}

+0

如果其重新载入那么其他按钮选择颜色应该改变...... –

+0

它是多重选择... –

+0

您是否只需要一次选择一个按钮? – Jamil

0

你可以做到这一点很容易跟随此方法。 每个按钮都有一个名为'selected'的属性。您可以使用它而不是为每个按钮创建一个BOOL变量。 假设你的按钮名称为“点击”,简单地通过

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

检查按钮的状态,并设置颜色,因为选择的是一个属性,它的BOOL价值是由它的内置函数处理,你不必设置到是或否。

编辑:

找到一个更简单的方法。 将选定和未选定按钮的图像作为资源添加到项目中并进行设置。对于状态:UIControlStateSelected];} [UIImageBaseNamed:@“selected.png”] forState:UIControlStateSelected]; [UIImage imageNamed:@“unselected.png”] forState:UIControlStateNormal];这是一个简单的例子。 `

+0

其不工作....... –

+0

你在哪里运行此检查? – Skywalker

+0

里面的@selector方法... –