嘿我正在一个屏幕上,用户有选项组例如“饮料”这是我的tableView部分的标题,他们的选择是“7up”,“可乐”等,这是我的桌子的细胞。UItableViewCells中的单选按钮逻辑
现在每个选项组选项(按单词顺序排列的每个单元格)都有一个单选按钮。我想要实现这一点。如果用户选择任何单元格的单选按钮,则会遇到问题,那么应该取消选择其他单选按钮,但是该怎么做?
请任何帮助
嘿我正在一个屏幕上,用户有选项组例如“饮料”这是我的tableView部分的标题,他们的选择是“7up”,“可乐”等,这是我的桌子的细胞。UItableViewCells中的单选按钮逻辑
现在每个选项组选项(按单词顺序排列的每个单元格)都有一个单选按钮。我想要实现这一点。如果用户选择任何单元格的单选按钮,则会遇到问题,那么应该取消选择其他单选按钮,但是该怎么做?
请任何帮助
您应该创建一个函数来检查您的自定义单元格中的单选按钮,并实现委托方法来通知您的TableViewController,该单元格上的按钮已被选中。
你的TableViewController需要实现该委托(不要忘记设置每个cell.delegate = self)。
然后在您的委托方法中,创建一个循环以取消选中除刚刚选中的单元格之外的所有单元格的单选按钮。
类似的东西:
这是一个自定义的UITableViewCell一个按钮。 的影像检查并取消需要看起来像一个单选按钮,检查和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的
//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];
}
}
现在只是适应.m文件您的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];
}
}
}
}
}
但我没有使用自定义单元格。我的单元格代码在cellForRowAtIndexPath方法中可用:( – user3422986
可以用我目前的代码在cellforrowatindexpath中随意调整,我将不胜感激 – user3422986
你如何在你的单元格中添加单选按钮? – FlavienSi
一个解决方案是利用表视图的本地选择功能。
在一个标准的UITableView中,一次只能选择一行,您可以使用它来获得优势。通过将故事板中的“选择”设置为“无”,行的选择将不可见。
现在您可以实现您自己的选择显示。您可以覆盖方法-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
以在您的单元格被选中时更新它。
而且您可以覆盖方法-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
以在不再选择单元格时更改该单元格。
UITableViewDelegate自动在旧选择上调用didSelectRowAtIndexPath
,当做出新的选择时,保持选择独一无二,如单选按钮。
我把一个小样本项目放在一起给你试试,你可以download it here。
希望我至少有一点帮助。
干杯!
了2选项的单选按钮的UITableView简单的解决方案(但你的想法):
- (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;
}
}
什么是您的实际问题?取消其他人的选择? –