1

I have followed this tutorial for RadioButton.I have integrated it with tableview.The problem is that when I select table row then radio button does not get selected. It gets selected only when I click on RadioButton. I want the radio button to get selected when table row is selected.The code is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }
    RadioButton *rb1 = [[RadioButton alloc] initWithGroupId:@"Group" index:indexPath.row];
    rb1.frame = CGRectMake(20,13,22,22);
    [cell.contentView addSubview:rb1];
    cell.textLabel.text = @"ABC";
    return cell;
}

RadioButton with Tableview

shakshi
  • 231
  • 1
  • 2
  • 9
  • put your selection code in -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } – Birendra Aug 10 '16 at 10:10

2 Answers2

1

do the following code on didSelectRowAtIndexPath

Objective C

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton *btn = (UIButton *)[cell viewWithTag:101];
        [btn setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected];
        btn.selected = YES;
        NSLog(@"Button : %@",btn);
    }



    - (UIImage *)imageWithColor:(UIColor *)color {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return image;
    }
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
  • I have added radio button in contentView of tableview cell. It is not a custom cell. So how to access the radio button in didSelectRowAtIndexPath. – shakshi Aug 10 '16 at 11:18
  • you should assign tag to your button (suppose 10), now you can get button like this "var yourbutton = cell.contentView.viewWithTag(10) as! UIButton" Once you get yourbutton rest of code are same – Anupam Mishra Aug 10 '16 at 11:27
  • I tried this but it is not able to set the button isSelected:YES. It is throwing error unrecognised selector sendt to instance – shakshi Aug 10 '16 at 12:25
  • We have used the radio button from the above mentioned tutorial.The code in didSelectRowAtIndexPath is - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIButton *btn = (UIButton *)[cell viewWithTag:101]; btn.backgroundColor=[UIColor redColor]; btn.selected = YES; NSLog(@"Button : %@",btn); [self radioButtonSelectedAtIndex:indexPath.row inGroup:@"Group"]; RadioButton *rb = [[RadioButton alloc] init]; //[rb handleButtonTap:self]; } – shakshi Aug 10 '16 at 12:40
  • This is also not working. Giving this error : [RadioButton setBackgroundImage:forState:]: unrecognized selector sent to instance – shakshi Aug 10 '16 at 12:57
  • There seems to be a mis-connection, please check this line UIButton *btn = (UIButton *)[cell viewWithTag:101]; giving you UIButton – Anupam Mishra Aug 10 '16 at 13:02
  • No, When i log btn it gives RadioButton instance. – shakshi Aug 10 '16 at 13:06
1

//Set tag to your Btn in "cellforRowAtIndexpath"

rb1.tag=101;

In didSelectRowAtIndexPath ->

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *btn = (UIButton*)[cell viewWithTag:101]; 
 btn.backgroundColor=[UIColor redColor];
btn.Selected = YES;
 [btn handleButtonTap:self];
  • I tried this but it is not able to set the button isSelected:YES. – shakshi Aug 10 '16 at 12:17
  • On clicking radio button the sender value is whereas when i click the tableview cell then sender value is > – shakshi Aug 10 '16 at 12:20
  • UIButton *btn = (UIButton*)[cell viewWithTag:101]; with this you can access your table's seleced row Button(rbi Button(Index)), – Varinder Singh iPhone Dev Aug 10 '16 at 12:28
  • We have used the radio button from the above mentioned tutorial.The code in didSelectRowAtIndexPath is - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIButton *btn = (UIButton *)[cell viewWithTag:101]; btn.backgroundColor=[UIColor redColor]; btn.selected = YES; NSLog(@"Button : %@",btn); [self radioButtonSelectedAtIndex:indexPath.row inGroup:@"Group"]; RadioButton *rb = [[RadioButton alloc] init]; //[rb handleButtonTap:self]; } – shakshi Aug 10 '16 at 12:40
  • this is complicated Library ,You can use simple button to Achieve this functionality, check updated Answer – Varinder Singh iPhone Dev Aug 10 '16 at 12:50
  • how to achieve radio button functionality then. – shakshi Aug 10 '16 at 12:53
  • if you want only one row selected At time -->http://stackoverflow.com/a/18002009/4970453 – Varinder Singh iPhone Dev Aug 10 '16 at 13:11
  • Thanks. pastie.org/10934067 fulfilled my need. – shakshi Aug 11 '16 at 10:03