My app has a multiple choice questions and answers type screen.
For this i have designed screen with UITableView i.e.,In a row i have taken four answers with four radio buttons. Here what is my exact problem is, if while I am selecting one radio button in one row its automatically selecting another row along with what I selected previous. Example in row1 i have selected option2 radio button then here its automatically selecting another row option2 also.
I have tried below code please help me on this. Thanks in Advance.
-(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;
}
In radio buttons actions i have tried this code:
-(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];
}
}