Gowtham K
Gowtham K

Reputation: 75

How to create a radio button in table view

I am having a radio button in table view cell. This is my radio button

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

 radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
    radiobtn.frame = CGRectMake(30, 0, 15, 14.5);

    [radiobtn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
    [radiobtn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
    [radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = radiobtn;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
 return cell;

}

-(void)radiobtn:(id)sender

{


 if([sender isSelected])

{
[sender setSelected:NO];
}  else
[sender setSelected:YES];
} }

In the above coding radio button is not changing to selected state. Please help me in coding.

Upvotes: 1

Views: 4812

Answers (3)

Mohd Prophet
Mohd Prophet

Reputation: 1521

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);

[newRadioButton setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
[newRadioButton addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = newRadioButton;
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;

}

Upvotes: 1

Fawad Masud
Fawad Masud

Reputation: 12344

Set the images for both selected and unselected state while creating the button.

radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 0, 15, 14.5);
[radiobtn setImage:[UIImage imageNamed:@"unselect"]] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:@"select"]] forState:UIControlStateSelected];
 radiobtn.tag=1;
[radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;

On button click, you just have to change the selected state.

-(void)radiobtn:(UIButton *)sender
{
    if([sender isSelected])
    {
        [sender setSelected:NO];
    }

     else
     {
         [sender setSelected:YES];
      }
}

Upvotes: 4

Daniyar
Daniyar

Reputation: 3003

  1. Set target action for the control event UIControlEventValueChanged.
  2. Set the image for certain state before the action happened (in cellForRowAtIndexPath method). By using the setImage:forState: you don't actually change the current control image, you just say the control use exact image for certain control state on that state occurred. Don't use highlighted state in case selected state is more appropriate. Try to change the state programmatically by [radiobtn setSelected:YES] or [radiobtn setSelected:!radiobtn.isSelected].

Upvotes: 0

Related Questions