【问题标题】:Radio button logic in UItableViewCellsUItableViewCells 中的单选按钮逻辑
【发布时间】:2014-05-29 04:51:54
【问题描述】:

嘿,我正在一个屏幕上工作,其中用户有选项组,例如“Drink”,这是我的 tableView 中的部分标题,他们的选择是“7up”、“coke”等,它们是我表格的单元格。

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

请帮忙

【问题讨论】:

  • 您遇到的实际问题是什么?取消选择其他人?

标签: ios objective-c uitableview


【解决方案1】:

您应该创建一个函数来检查您的自定义单元格中的单选按钮,并实现一个委托方法来通知您的 TableViewController 您选择了该单元格上的按钮。

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

然后在您的委托方法中创建一个循环以取消选中该部分中除您刚刚选中的单元格之外的所有单元格的单选按钮。

类似的东西:

这是一个带有按钮的自定义 UITableViewCell。 选中和取消选中的图像需要看起来像选中和取消选中的单选按钮 这是 .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];
     }
}

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

//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 方法中可用:(
  • 能否用我当前在 cellforrowatindexpath 中的代码进行调整,我将不胜感激
  • 如何在单元格中添加单选按钮?
  • 我刚刚编辑了代码,这样一切正常。如果您有任何问题,请不要犹豫。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    一种解决方案是利用表格视图的原生选择功能。

    在标准 UITableView 中,一次只能选择一行,您可以充分利用这一点。通过将情节提要中的“选择”设置为“无”,行的选择将不可见。

    现在您可以实现自己的选择显示。您可以覆盖方法 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 以在您的单元格被选中时更新它。

    当单元格不再被选中时,您可以覆盖方法-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 来更改单元格。

    UITableViewDelegate 自动在旧选择上调用didSelectRowAtIndexPath,当做出新选择时,保持选择的唯一性就像单选按钮一样。

    我整理了一个小样例项目给你试试,你可以download it here

    希望我至少能帮上一点忙。

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多