我的应用有多种选择问题和答案类型的屏幕。
为此我设计了带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];
}
}

答案 0 :(得分:0)
您的代码中有许多区域可以改进,小的东西可能会导致这种行为。我发布了修改后的代码。请用我的代码替换你的代码块。代码中的问题是全局声明了单元格并引用了全局声明的表视图。在点击按钮检测单元格的情况下,我希望您做的最有效的改进。您不需要这么多代码就可以实现这一目标。让我们开始。
为了保持选择状态,我们将使用数组sectionData。
将此代码添加到did load函数。
for (int index = 0; index < 10; index ++) {//taken 10 because you are using only 10 rows
[sectionData addObject:@"0"];
}
并替换以下功能的代码完全。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView 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 setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"1"])];
[cell.option2RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"2"])];
[cell.option3RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"3"])];
[cell.option4RadioBtn setSelected:([[sectionData objectAtIndex:indexPath.row] isEqualToString:@"4"])];
cell.option1RadioBtn.indexPath = indexPath;
cell.option2RadioBtn.indexPath = indexPath;
cell.option3RadioBtn.indexPath = indexPath;
cell.option4RadioBtn.indexPath = indexPath;
[cell.option1RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.option2RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.option3RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.option4RadioBtn addTarget:self action:@selector(radioBtnnAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
您可以看到我只使用函数内部的变量引用。我已经将目标仅作为一个功能。现在你需要做的诀窍是创建UIButton的子类并将其命名为CustomButton。并且还在上面看到我已经将indexPath分配给按钮属性的行。 不要忘记在UITableViewCell的故事板和类文件中更改Outlet和Element的类。
自定义按钮的.h文件。
@interface CustomButton : UIButton
@property (nonatomic, strong) NSIndexPath *indexPath;
@end
现在您的目标函数将如下所示。
- (void)radioBtnnAction:(CustomButton*)sender
{
UITableViewCell *cell = [self.tableViewRef cellForRowAtIndexPath:sender.indexPath];
[sectionData replaceObjectAtIndex:sender.indexPath.row withObject:((sender == cell.option1RadioBtn)?@"1":(sender == cell.option2RadioBtn)?@"2":(sender == cell.option3RadioBtn)?@"3":@"4")];
[cell.option1RadioBtn setSelected:(sender == cell.option1RadioBtn)];
[cell.option2RadioBtn setSelected:(sender == cell.option2RadioBtn)];
[cell.option3RadioBtn setSelected:(sender == cell.option3RadioBtn)];
[cell.option4RadioBtn setSelected:(sender == cell.option4RadioBtn)];
}
我希望我已经包含了所有内容。试一次。
答案 1 :(得分:0)
您似乎已经有了自定义单元格。
而不是在单元格上暴露标签questionLbl。 让细胞采用问题模型。 该模型可能类似于
类问题模型 财产问题 物业选择
您的单元格有一个property = questionModel AND处理按钮选择。因此,当您使用单元属性设置器分配问题模型时,也可以设置按钮状态。在单元格中有按钮目标/动作调用方法,用于更新问题模型选择属性。
您可以在tableView源中设置cell.question,而不是直接设置标签。
底线是,单元格应该处理模型而不是viewController。只是将模型传递给单元格。