UItableViewCells中的单选按钮逻辑

时间:2014-03-25 15:05:37

标签: ios objective-c uitableview

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

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

任何帮助,请

3 个答案:

答案 0 :(得分:2)

您应该创建一个函数来检查自定义单元格中的单选按钮,并实现委托方法以通知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];
                   }
             }
         }     
     }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

一种解决方案是利用表格视图本机选择功能。

在标准的UITableView中,一次只能选择一行,您可以利用它来获得优势。通过将故事板中的“选择”设置为“无”,将无法看到行的选择。

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

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

当进行新的选择时,UITableViewDelegate会自动调用旧选择中的didSelectRowAtIndexPath,使选择保持唯一,如单选按钮。

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

希望我至少有一点帮助。

干杯!