我的应用程序有多个选择题和答案类型屏幕。一组单选按钮在同一行中选择一个单选按钮,它影响另一行 - 目标-c
为此,我设计了UITableView屏幕,即在一行中,我用四个单选按钮拍摄了四个答案。 这里我确切的问题是,如果当我在一行中选择一个单选按钮时,它会自动选择另一行以及我之前选择的行。在row1中的示例我已经选择了option2单选按钮,在这里它也自动选择另一行option2。
我已经尝试了下面的代码,请帮我对此。 在此先感谢。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [_tableViewRef dequeueReusableCellWithIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *rowNumber = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
[[cell questionLbl] setText:[NSString stringWithFormat:@"%@. %@",rowNumber,@"Provide your own fixed background behind the UITableView?"]];
[cell.option1RadioBtn addTarget:self
action:@selector(radioBtnn1Action:)
forControlEvents:UIControlEventTouchUpInside];
[cell.option2RadioBtn addTarget:self
action:@selector(radioBtnn2Action:)
forControlEvents:UIControlEventTouchUpInside];
[cell.option3RadioBtn addTarget:self
action:@selector(radioBtnn3Action:)
forControlEvents:UIControlEventTouchUpInside];
[cell.option4RadioBtn addTarget:self
action:@selector(radioBtnn4Action:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
在单选按钮的动作我已经试过这段代码:
-(void)radioBtnn1Action:(UIButton*)sender
{
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef];
NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint];
cell = [self.tableViewRef cellForRowAtIndexPath:indexPath];
if ([cell.option1RadioBtn isSelected]) {
[cell.option1RadioBtn setSelected:YES];
[cell.option2RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
else
{
[cell.option1RadioBtn setSelected:YES];
[cell.option2RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
}
-(void)radioBtnn2Action:(UIButton*)sender
{
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef];
NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint];
cell = [self.tableViewRef cellForRowAtIndexPath:indexPath];
if ([cell.option2RadioBtn isSelected]) {
[cell.option2RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
else
{
[cell.option2RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
}
-(void)radioBtnn3Action:(UIButton*)sender
{
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef];
NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint];
cell = [self.tableViewRef cellForRowAtIndexPath:indexPath];
if ([cell.option3RadioBtn isSelected]) {
[cell.option3RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option2RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
else
{
[cell.option3RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option2RadioBtn setSelected:NO];
[cell.option4RadioBtn setSelected:NO];
}
}
-(void)radioBtnn4Action:(UIButton*)sender
{
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.tableViewRef];
NSIndexPath *indexPath = [self.tableViewRef indexPathForRowAtPoint:rootViewPoint];
cell = [self.tableViewRef cellForRowAtIndexPath:indexPath];
if ([cell.option4RadioBtn isSelected]) {
[cell.option4RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option2RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
}
else
{
[cell.option4RadioBtn setSelected:YES];
[cell.option1RadioBtn setSelected:NO];
[cell.option2RadioBtn setSelected:NO];
[cell.option3RadioBtn setSelected:NO];
}
}
您应该为单选按钮使用标签。像这样:=> option1RadioBtn.tag = 1等等。而radioBtnn1Action函数验证如果option1RadioBtn.tag == 1这样的东西。 – iParesh
嘿@ iParesh感谢您的回复,请你在这里发布一些代码片段。 – Sajida
你需要创建一个nsmutablearray默认情况下添加4个单选按钮值0.hwen用户可以选择任何一个设置1值用于特定的单选按钮索引值因为这个问题是tableview可重复使用的单元格问题 – iOS