How to select radio button at a time in table view

2019-09-08 04:43发布

问题:

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).

I need to make it look like the first image.

回答1:

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



回答2:

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

for (UITableViewCell *cell in [self visibleCells]) {
    [[cell accessoryView] setSelected:NO];
}


回答3:

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]);   
}


回答4:

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;
}