【问题标题】:dynamic radio buttons uitableview custom cell ios动态单选按钮uitableview自定义单元格ios
【发布时间】:2017-06-18 19:24:31
【问题描述】:

我想使用自定义单元格创建动态单选按钮。我尝试并能够使用故事板创建。但是如何只允许一个单选按钮选择?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   MChangeServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"changeServiceCellidentifier"];

   cell.serviceLabelView.text =@"test";



   cell.radioButton.tag=indexPath.row;


   [cell.radioButton addTarget:self action:@selector(radioButtonColorChange:) forControlEvents:UIControlEventTouchUpInside];

      return cell;
}

- (void)radioButtonColorChange:(id)sender
{
   UIButton *button=(UIButton *) sender;
   NSIndexPath *indexpath = [NSIndexPath indexPathForRow:button.tag inSection:0];
   MChangeServiceTableViewCell *tappedCell = (MChangeServiceTableViewCell *)[self.listTableView cellForRowAtIndexPath:indexpath];
   if ([tappedCell.radioButton.imageView.image isEqual:[UIImage imageNamed:@"radio_button_unselected.png"]])
   {
      [tappedCell.radioButton setImage:[UIImage imageNamed:@"radio_button_selected.png"] forState:UIControlStateNormal];
   }
   else
   {
      [tappedCell.radioButton setImage:[UIImage imageNamed:@"radio_button_unselected.png"] forState:UIControlStateNormal];
   }
}

我可以通过单击来更改单选按钮的图像。但是单击一个按钮时如何将剩余的单选按钮更改为未选中状态?

【问题讨论】:

标签: ios objective-c


【解决方案1】:

我建议您将当前选定的按钮保存在另一个变量中。

@interface YourViewController ()
@property (strong, nonatomic) UIButton *currentBtn;
@property (nonatomic) NSInteger currentSelectedIndex;

@end

然后

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   _currentSelectedIndex = -1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     MChangeServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"changeServiceCellidentifier"];

     cell.serviceLabelView.text =@"test";

     cell.radioButton.tag=indexPath.row;

     [cell.radioButton setImage:[UIImage imageNamed:@"radio_button_selected.png"] forState:UIControlStateSelected];
     [cell.radioButton setImage:[UIImage imageNamed:@"radio_button_unselected.png"] forState:UIControlStateNormal];
     [cell.radioButton addTarget:self action:@selector(radioButtonColorChange:) forControlEvents:UIControlEventTouchUpInside];

     [cell.radioButton setSelected:NO];

     if (indexPath.row == _currentSelectedIndex) {
         [cell.radioButton setSelected:YES];
         _currentBtn = cell.radioButton;
     }

     return cell;
}

- (void)radioButtonColorChange:(id)sender
{
   _currentBtn.selected = NO;
   UIButton *button=(UIButton *) sender;
   button.selected = !button.isSelected;
   _currentBtn = button;
   _currentSelectedIndex = button.tag;
}

【讨论】:

  • ,我使用了上面的代码,我可以选择单选按钮。但是当我滚动表格视图时,我可以看到底部的单元格有一个单元格单选按钮处于选中状态。
  • 还是一样的结果。其他单元格正在选择按钮图像。
  • 现在检查我编辑了答案,如果我的答案有帮助,您可以标记为已接受并投票
  • 非常感谢。它对我帮助很大。最后一个问题,如果我想在同一个 tableviewcell 中再添加一个按钮并在某些条件下启用/禁用它(与单选按钮无关)。我应该遵循相同的方法吗?任何想法。
  • 是的,你可以用同样的方法做到这一点
猜你喜欢
  • 1970-01-01
  • 2015-06-13
  • 2011-11-27
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多