UItableViewCells中的单选button逻辑

嘿,我正在一个屏幕上,用户有选项组例如“饮料”,这是我的tableView部分的标题,他们的select是“7up”,“可乐”等我的表的单元格。

现在每个选项组选项(按单词顺序排列的每个单元格)都有一个单选button。 我想要实现这个。 我遇到问题,如果用户select任何单元格的单选button,然后应该取消select其他单选button,但如何?

请任何帮助

您应该创build一个函数来检查您的自定义单元格中的单选button,并实现一个委托方法来通知您的TableViewController,您在该单元格上的button被选中。

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

然后在你的委托方法中,你创build一个循环来取消除了你刚才检查的单元格之外的所有单元格的单选button。

类似的东西:

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

//RadioCell.h @protocol RadioCellDelegate <NSObject> -(void) myRadioCellDelegateDidCheckRadioButton:(RadioCell*)checkedCell; @end @interface RadioCell : UITableViewCell -(void) unCheckRadio; @property (nonatomic, weak) id <RadioCellDelegate> delegate; @end 

这是RadioCell的.m文件

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

现在只需调整你的tableview控制器RadioCell(在.m文件中)

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

简单的解决scheme2选项单选buttonUITableView(但你明白了):

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSIndexPath *newIP; if (!indexPath.row) newIP = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; else newIP = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) cell.accessoryType = UITableViewCellAccessoryNone; else{ cell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *newCell = [tableView cellForRowAtIndexPath:newIP]; newCell.accessoryType = UITableViewCellAccessoryNone; } } 

一种解决scheme是利用表视图的本地selectfunction。

在标准的UITableView中,一次只能select一行,你可以使用这个优点。 通过将故事板中的“select”设置为“无”,行的select将不可见。

现在你可以实现你自己的select显示。 你可以重写方法-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath来更新你的单元格,当它被选中。

你可以重写方法-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath当它不再被选中时,改变单元格。

UITableViewDelegate自动调用旧select上的didSelectRowAtIndexPath ,当做出新的select时,保持select独一无二的单选button。

我把一个小样本项目放在一起给你试试,你可以在这里下载 。

希望我至less有一点帮助。

干杯!