动态单选按钮uitableview自定义单元ios [英] dynamic radio buttons uitableview custom cell ios

查看:41
本文介绍了动态单选按钮uitableview自定义单元ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create dynamic radio buttons using custom cell .I tried and able to create using story board. but how to allow only one radio button selection?

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

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

I am able to change the image of the radio button by clicking on it.But how to change the remaining radio buttons to unchecked state when one button is clicked?

推荐答案

我建议你将当前选中的按钮保存在另一个变量中.

I suggest you so save current selected button in another variable.

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

这篇关于动态单选按钮uitableview自定义单元ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆