UItableViewCells中的单选按钮逻辑 [英] Radio button logic in UItableViewCells

查看:131
本文介绍了UItableViewCells中的单选按钮逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我正在一个用户有选项组的屏幕上工作,例如Drink,这是我的tableView中节的标题,他们的选择是7up,coke等,这些是我桌子的单元格。

Hey I'm working on a screen where user have option groups for example "Drink" which is title of section in my tableView, and their choices are "7up", "coke", etc which are cells of my table.

现在每个选项组选项(按顺序单词中的每个单元格)都有一个单选按钮。我想实现这个。如果用户选择任何单元格的单选按钮,我将面临问题,那么应该取消选择其他单选按钮但是如何?

Now every Option Group choice (every cell in order words) has one radio button. I want to implement this. I'm facing problem if user selects any cell's radio button then other radio buttons should be deselected but how?

任何帮助请

推荐答案

你应该创建一个功能来检查您的自定义单元格中的单选按钮,并实现委托方法,以通知您的TableViewController您的单元格上的按钮被选中。

You should create a function to check your radio button from your custom cell and implements a delegate method to inform your TableViewController that your button on that cell was selected.

您的TableViewController需要实现该委托(不要忘记)设置每个cell.delegate = self)。

Your TableViewController needs to implements that delegate (dont forget to set each cell.delegate = self).

然后在你的委托方法中创建一个循环来取消选中除了单元格之外的部分中单元格的所有单选按钮你刚刚检查过。

Then in your delegate method you create a loop to uncheck all of the radio buttons of the cells in the section except the cell you just checked.

类似的东西:

这是一个带按钮的自定义UITableViewCell。
选中和取消选中的图像需要看起来像一个单选按钮检查并取消选中
这是.h文件:

This is a custom UITableViewCell with a button. The images checked and uncheck need to look like a radio button checked and uncheked Here is the .h file :

//RadioCell.h
@protocol RadioCellDelegate <NSObject>
    -(void) myRadioCellDelegateDidCheckRadioButton:(RadioCell*)checkedCell;
@end

@interface RadioCell : UITableViewCell
     -(void) unCheckRadio;
     @property (nonatomic, weak) id <RadioCellDelegate> delegate;
@end

这是RadioCell的.m文件

This is the .m file of RadioCell

//RadioCell.m
@property (nonatomic, assign) UIButton myRadio;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
   _myRadio = [UIButton buttonWithType:UIButtonTypeCustom];
   [_myRadio setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
   [_myRadio setImage:[UIImage imageNamed:@"check"] UIControlStateSelected];
   [_myRadio addTarget:self action:@selector(radioTouched)orControlEvents:UIControlEventTouchUpInside];
   _myRadio.isSelected = NO;

   //don't forget to set _myRadio frame
   [self addSubview:_myRadio];
}

-(void) checkRadio {
   _myradio.isSelected = YES;
}

-(void) unCheckRadio {
   _myradio.isSelected = NO;
}

-(void) radioTouched {
     if(_myradio.isSelected == YES) {
          return;
     } 
     else {
       [self checkRadio]
       [_delegate myRadioCellDelegateDidCheckRadioButton:self];
     }
}

现在只需使用RadioCell调整你的tableview控制器(in。 m文件)

Now just adapt your tableview controller with RadioCell (in .m file)

//MyTableViewController.m

@interface MyTableViewController () <RadioCellDelegate>
@end

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
   {
      static NSString *CellIdentifier = @"RadioCell";
      RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[RadioCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }

      cell.textLabel = @"Coke"; //or whatever you want
      cell.delegate = self;

      return cell;
   }

-(void) myRadioCellDelegateDidCheckRadioButton:(RadioCell*)checkedCell {
     NSIndexPath *checkPath = [self.tableView indexPathForCell:checkedCell];

     for (int section = 0; section < [self.tableView numberOfSections]; section++) {
         if(section == checkPath.section) {
             for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
                   NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
                   RadioCell* cell = (CustomCell*)[tableView cellForRowAtIndexPath:cellPath];

                   if(checkPath.row != cellPath.row) {
                       [cell unCheckRadio];
                   }
             }
         }     
     }
}

这篇关于UItableViewCells中的单选按钮逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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