Siva Developer Android iOS Siva Developer Android iOS - 1 year ago 107
iOS Question

Radio button inside the UITableview not working

I wish to add radio button to UITableview. The function of radio button should be like when I select radio button in the table cell that radio button should be selected and other all radio buttons in the other cell should be unselected. It should be kind of either or, that is only one radio button can select.

Answer Source

create the one int value

int selectstr;
 UIButton * button;

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;


UILabel  * title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 14.0, 215.0, 36.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor=[UIColor blackColor];
title.text = @"TEST";
// title.font = [UIFont fontWithName:@"Arial Hebrew" size:16.0f];
 title.font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
[cell.contentView addSubview:title];

button = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 46.0f, 30.0f)];

if (selectstr ==indexPath.row)
    [button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];
    cell.backgroundColor=[UIColor colorWithRed:(243.0/255.0) green:(114.0/255.0) blue:(74.0/255.0) alpha:1.0f];

    [button setImage:[UIImage imageNamed:@"radio_unselected.png"] forState:UIControlStateNormal];
    cell.backgroundColor=[UIColor clearColor];

[button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];

return cell;


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

[tableView reloadData];


  - (IBAction)toggleCheckedMode:(UIButton*)sender

   [button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];

    // here customized for your self
     [tableView reloadData];