2017-02-27 104 views
0

我的应用程序有多个选择题和答案类型屏幕。一组单选按钮在同一行中选择一个单选按钮,它影响另一行 - 目标-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]; 
 
     
 
    } 
 
    
 
    
 
}

+0

您应该为单选按钮使用标签。像这样:=> option1RadioBtn.tag = 1等等。而radioBtnn1Action函数验证如果option1RadioBtn.tag == 1这样的东西。 – iParesh

+0

嘿@ iParesh感谢您的回复,请你在这里发布一些代码片段。 – Sajida

+0

你需要创建一个nsmutablearray默认情况下添加4个单选按钮值0.hwen用户可以选择任何一个设置1值用于特定的单选按钮索引值因为这个问题是tableview可重复使用的单元格问题 – iOS

回答

0

有很多地区在你的代码,你可以改善和小事情会导致这种行为。我的发布代码与修改。请用我的代码替换你的代码块。代码中的问题是您有全局声明该单元格并采用全局声明的表格视图的引用。在按钮点击检测单元的情况下,我希望你做的最有效的改进。你不需要这么多的代码来实现这一点。开始吧。

为了保持选择状态,我们将使用数组sectionData。

将此代码添加到加载函数。

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; 
 
}

您可以看到使用变量的引用只在函数内部我。我只将目标作为1个功能。现在你需要做的技巧是创建一个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)[email protected]"1":(sender == cell.option2RadioBtn)[email protected]"2":(sender == cell.option3RadioBtn)[email protected]"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)]; 
 
}

我希望我已经包括了一切。尝试一次。

+0

我错过了我的代码中的东西。我编辑了答案。请使用cellForRowAtIndexPath的最新代码块 –

+0

嗨,谢谢你,但在这里[cell.option1RadioBtn setIndexPath:indexPath]; 显示了一些错误,如* UButton的* no visible @ interface声明选择器'setIndexpath'* – Sajida

+0

请参考答案中的粗体行。我提到你要将这些按钮的类改为CustomButton。 –

0

看起来您已经拥有一个自定义单元格。

而不是在单元上暴露标签questionLbl。 让单元格采用问题模型。 该模型可以像

类QuestionModel 物业问题 属性选择

你的电池有一个属性= questionModel 和手柄的按钮选择。因此,当您使用单元格属性设置器分配问题模型时,也可以设置按钮状态。在更新问题模型选择属性的单元格中有按钮目标/操作调用方法。

您在tableView源文件中设置了cell.question,而不是直接设置标签。

底线是,单元格应该处理模型而不是viewController。它只是将模型传递给单元格。

+0

在重新阅读您的问题时,问题模型应该包含答案,因为属性甚至可以作为数组。问题的关键在于,QuestionModel应该具有显示状态并记录状态变化所需的所有信息。 – Taun

相关问题