我的應用程序有多個選擇題和答案類型屏幕。一組單選按鈕在同一行中選擇一個單選按鈕,它影響另一行 - 目標-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