【发布时间】:2017-02-27 05:18:35
【问题描述】:
我的应用有一个选择题和答案类型屏幕。
为此,我设计了带有 UITableView 的屏幕,即,我连续使用四个单选按钮获得了四个答案。 我的确切问题是,如果当我在一行中选择一个单选按钮时,它会自动选择另一行以及我之前选择的内容。第 1 行中的示例我选择了 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 感谢您的回复,请您在此处发布一些代码sn-p。
-
你需要创建一个nsmutablearray而不是默认添加4个单选按钮值0.hwen用户可以为特定单选按钮索引值选择任何一个设置1个值,因为这个问题是tableview reusablecell问题
-
嘿@Jigar,谢谢,这里有一些代码
-
@Sajida 检查此stackoverflow.com/questions/35402287/…。在这个问题中你有一些想法。如果有任何问题,请告诉我
标签: ios objective-c uitableview radio-button