1

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;

        UIButton *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];
        cell.accessoryView = newRadioButton;

        if ([indexPath isEqual:selectedIndex]) 
        {
            newRadioButton.selected = YES;
        } 
        else 
        {
            newRadioButton.selected = NO;
        }
    }
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndex = indexPath;
    [table reloadData];
}

-(void)radiobtn:(id)sender
{
    if([sender isSelected])
    {
        [sender setSelected:NO];
    } else
    {
        [sender setSelected:YES];
    }
}

Its working,But i want to select only one radio button at a time like this image (left). But for me selecting all the radio button (right).

image correct image wrong

I need to make it look like the first image.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Gowtham K
  • 75
  • 2
  • 9

4 Answers4

2

I think you should try:

in YourViewController.h

  @interface YourViewController : UITableViewController {
     NSIndexPath *selectedIndex
    } 

in YourViewController.m

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        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];
        cell.accessoryView = newRadioButton;
     }
       if ([indexPath isEqual:selectedIndex]) {
         newRadioButton.seleted = YES; 
       } else {
         newRadioButton.seleted = NO;
       }
   cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
  }

and

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        selectedIndex = indexPath;
        [tableView reloadData];
 }

this link may help you

Community
  • 1
  • 1
Huy Nghia
  • 996
  • 8
  • 22
  • 1
    @GowthamK I know why. You don't need add selector for newRadioButton anymore remove -(void)radiobtn:(id)sender and [newRadioButton addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside]; – Huy Nghia Feb 20 '15 at 09:10
  • i have removed that bro, but now radio button is not changing – Gowtham K Feb 20 '15 at 09:15
  • @GowthamK see my edited. replace your cellForRowAtIndexPath: code with mine – Huy Nghia Feb 20 '15 at 09:31
  • I have changed once again your edited code, still is not working, why ? – Gowtham K Feb 20 '15 at 09:36
  • see my code not have cell.selectionStyle = UITableViewCellSelectionStyleNone; and if ([indexPath isEqual:selectedIndex]) not in if(cell == nil) {} – Huy Nghia Feb 20 '15 at 09:40
  • bro i have deleted cell.selectinstyle and if condition is out of the if(cell == nil). i replace your code only, its not working – Gowtham K Feb 20 '15 at 09:45
  • did you load cell from storyboard ? – Huy Nghia Feb 20 '15 at 09:55
  • no my table view is programatically done. this like i set my table view self.table = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 0) style:UITableViewStylePlain]; self.table.backgroundColor=[UIColor whiteColor]; self.table.dataSource = self; self.table.delegate = self; [self.view addSubview:self.table]; – Gowtham K Feb 20 '15 at 10:07
0

Unselect all the radio buttons before your if statement using something like this:

for (UITableViewCell *cell in [self visibleCells]) {
    [[cell accessoryView] setSelected:NO];
}
Cole
  • 720
  • 1
  • 8
  • 18
0

When you are creating the button in cellForRowAtindexpth, attach an unique feature by which you can identify which button is being tapped. To do this UIButton has tag property

you need to set the tag for everybutton inside cellForRowAtindexpth, something like

// where i is just a static int variable for holding the count

myRadioButton.tag = i ;
 i=i+1 

now you when you tap based on tag, you can check which button is pressed and you can select that one to know which button is pressed u can know like this

-(IBAction)buttonPressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);   
}
beresfordt
  • 5,088
  • 10
  • 35
  • 43
Saket Kumar
  • 1,157
  • 2
  • 14
  • 30
0

Try to reuse button

Solution: we can add & access controls by tag from view

  1. if control found by tag we can use it.
  2. if control not found we have to add it with tag.
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
    UIButton *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];
    cell.accessoryView = newRadioButton;
}
Community
  • 1
  • 1
kalpesh jetani
  • 1,775
  • 19
  • 33