如何在表格视图中一次select单选button

我在表视图单元格中有一个单选button。 这是我的单选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]; } } 

它的工作,但我想一次只select一个单选button,像这个图像(左)。 但是,对于我select所有的单选button(右)。

图像正确图像错误

我需要使它看起来像第一个图像。

我认为你应该尝试:

在YourViewController.h中

  @interface YourViewController : UITableViewController { NSIndexPath *selectedIndex } 

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

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

这个链接可能会帮助你

在你的if语句之前,用下面的方法取消选中所有的单选button:

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

在cellForRowAtindexpth中创buildbutton时,附加一个独特的function,通过该function您可以识别正在点击哪个button。 为此,UIButton具有标签属性

你需要为cellForRowAtindexpth中的everybutton设置标签,就像

 // where i is just a static int variable for holding the count myRadioButton.tag = i ; i=i+1 

现在当你基于标签点击时,你可以检查哪个button被按下,你可以select那个button来知道哪个button被按下,你可以像这样知道

 -(IBAction)buttonPressed:(id)sender{ UIButton *button = (UIButton *)sender; NSLog(@"%d", [button tag]); } 

尝试重用button

解决scheme:我们可以通过视图中的标签添加和访问控件

  1. 如果通过标签find控件,我们可以使用它。
  2. 如果没有find控件,我们必须添加标签。
 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; }