我有一个分段的Uitableview,每个部分我有一个问题(UILabel),5个Uibuttons作为radiobuttons。 我将动态获得一些radoibutton。当我选择一个单选按钮时,Iam会突出显示状态,但如果我在该部分中单击另一个单选按钮,则会突出显示两个单选按钮。
但这不是正确的方式,我希望显示该部分中的所有单选按钮必须取消选中,必须突出显示所选的。我可以这样做吗
当radiobutton点击时,这是我的代码
-(IBAction) radioButtonClicked:(UIButton *) sender{
NSMutableArray *arr1=[[NSMutableArray alloc]init];
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
NSLog(@"The section is %d", indexPath.section);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
int n=[cellSection count]-3;
int m=[cellSection count]-1;
NSString *questionId=[[cellSection objectAtIndex:m-1]objectForKey:@"QId"];
for (NSDictionary *dict in selectedOptionandQIdArray) {
NSString *str=[dict valueForKey:@"QId"];
if(str==NULL)
{
}
else{
[arr1 addObject:str];
}
}
BOOL isTheObjectThere = [arr1 containsObject:questionId];
if(isTheObjectThere==YES)
{
NSInteger Aindex=[arr1 indexOfObject:questionId];
if(NSNotFound != Aindex)
{
NSLog(@" found");
[selectedOptionandQIdArray removeObjectAtIndex:Aindex];
}
}
NSLog(@"%d",sender.tag);
int tagID=[sender tag];
int itemtoAdd=[sender tag];
NSString *xWrapped=[NSString stringWithFormat:@"raid%d",itemtoAdd];
NSMutableDictionary *hk=[[NSMutableDictionary alloc]init];
[hk setObject:xWrapped forKey:@"Ans"];
[hk setObject:questionId forKey:@"QId"];
[hk setObject:@"" forKey:@"TestOption"];
[selectedOptionandQIdArray addObject:hk];
NSMutableArray *radioButtonsinaSection=[[NSMutableArray alloc]init];
if(n==5)
{
UIImage *image=[UIImage imageNamed:@"radio_button_off.png"];
NSLog(@"%d",tagID);
UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:11];
UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:12];
UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:13];
UIButton *btnTemp4=(UIButton *)[cell viewWithTag:14];
UIButton *btnTemp5=(UIButton *)[cell viewWithTag:15];
[radioButtonsinaSection addObject:btnTemp1];
[radioButtonsinaSection addObject:btnTemp2];
[radioButtonsinaSection addObject:btnTemp3];
[radioButtonsinaSection addObject:btnTemp4];
[radioButtonsinaSection addObject:btnTemp5];
}
[sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal];
for(int i=0;i<[radioButtonsinaSection count];i++){
[[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
}
}
答案 0 :(得分:0)
试试吧......
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int buttonTag = indexPath.row + 200;
//NSLog(@"%i",buttonTag);
UIButton *tempBtn = (UIButton *)[self.view viewWithTag:buttonTag];
[tempBtn setBackgroundImage:[UIImage imageNamed:@"checkbox_check.png"]forState:UIControlStateNormal];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
int buttonTag = indexPath.row + 200;
NSLog(@"%i",buttonTag);
UIButton *tempBtn = (UIButton *)[self.view viewWithTag:buttonTag];
[tempBtn setBackgroundImage:[UIImage imageNamed:@"checkbox.png"]forState:UIControlStateNormal];
}