我有一个 UITableViewCell,我在其中创建了一些按钮(它们的作用类似于单选按钮)并将标签分配给按钮。但是标签不是静态的。它们是动态的。现在,当单击特定单选按钮时,我需要使用一些图像(radio_On.png)设置该按钮并将所有按钮保留为(radio_Off.png)。但是要设置图像,我无法理解如何获取该特定单元格中所有按钮的标签值,因为它们不是静态的。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)];
[btTemp1 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp1 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp1.tag=++Optionid;
[hlcell.contentView addSubview:btTemp1];
btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)];
[btTemp2 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btTemp2 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
[btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];
btTemp2.tag=++Optionid;
[hlcell.contentView addSubview:btTemp2];
}
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
[sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal];
}
当我检查 TableView 单元格中的其他按钮时,这里没有释放一个单选按钮。
编辑:
以前我给了这个代码:
-(IBAction) radioButtonClicked:(UIButton *) sender {
UIButton *button = (UIButton *)sender;
NSLog(@"%d", [button tag]);
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
[[sender superview] superview]];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
UIButton *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic
UIButton *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic
UIButton *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic
UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic
UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic
[radioButtonsinaSection addObject:btnTemp1];
[radioButtonsinaSection addObject:btnTemp2];
[radioButtonsinaSection addObject:btnTemp3];
[radioButtonsinaSection addObject:btnTemp4];
[radioButtonsinaSection addObject:btnTemp5];
}
for(int i=0;i<[radioButtonsinaSection count];i++){
[[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
}
[sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal];
}
如何获取单击按钮(单选按钮)的单元格的所有控件(按钮及其标签)?