【问题标题】:Only One RadioButton is Selectable for a Column Group in UITableViewUITableView 中的列组只能选择一个 RadioButton
【发布时间】:2016-10-21 06:27:32
【问题描述】:

在问这个问题之前,我搜索了很多,但找不到合适的答案。

我有一个 UITableViewn 列数。
问题是列是根据键分组的。所以我的 UITableView 标题被分成两部分,一个为column group,而这个列组被划分为显示columns
我为cell 创建了一个自定义类,我在每列中显示radio button

代码:

cellForRowAtIndexPath方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GridTableViewCell *cell = nil; // My custom class for cell

    CGFloat startX = 230;

    if (cell == nil)
   {
       cell = [[GridTableViewCell alloc] initWithStyle1:UITableViewCellStyleDefault reuseIdentifier:@"Cell" withRows:[self.rowHeaderArray objectAtIndex:indexPath.row]];

       for (int i = 0; i < [self.groupColumNmeArray count]; i++) { // groupColumNmeArray contains the Column Group name

       NSArray *values = [_subColumnDict objectForKey:[self.groupColumNmeArray objectAtIndex:i]]; // subColumnDict contains the value for each column 

       self.originalDataArray = [CustomTableHeaderParser parseColumnInfo:values];// Parsing the values array for getting column name, type, width etc

       for (CustomTableColumn *column in self.originalDataArray)
       {

           switch (column.columnType) // acc. to column type display the cell with values.
           {
               case RadioBtn:
               {

                   _btTemp = [[UIButton alloc]initWithFrame:CGRectMake(startX, 2, subCellWidth , 40)];

                   [_btTemp setTag:indexPath.row];//indexPath.row];
                   [_btTemp addTarget:self action:@selector(radioButtonsClicked:) forControlEvents:UIControlEventTouchUpInside];
                   [_btTemp setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
                   [_btTemp setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                   _btTemp.titleLabel.font =[UIFont systemFontOfSize:14.f];

                   [self.radioButtons addObject:_btTemp];

                   [cell addSubview:_btTemp];

               }
                   break;

           startX += 2+subCellWidth;
        }
    }
}

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.opaque = YES;

return cell;

}

单选按钮点击动作方法;

-(IBAction) radioButtonsClicked:(UIButton *) sender {

    if ([sender isSelected]) {

        [sender setSelected: NO];
        [sender setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];

    } else {

        [sender setSelected: YES];
        [sender setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateNormal];
    }
    NSLog(@"BUTTON TAG  %ld",(long)sender.tag);
} 

所以我可以选择所有列中的单选按钮,但实际上我想要的是对于每个列组,只有一个单选按钮是可选的

谁能帮帮我。任何帮助都将不胜感激。

【问题讨论】:

  • No.. 对我来说按钮选择/取消选择工作正常,但我想说的是,如果我的 columnGroup 之一有三个子列,所以我在每个子单元格中创建了按钮,但我只想选择一个按钮在该列组下
  • 在您的单元格中,您有一组单选按钮,对吗?并且您只想允许对单个单元格进行单选
  • 我认为您还需要在 radioButtonsClicked 事件中保持其他按钮状态(选中、取消选中)。
  • @rohit Sidpara 你能举个例子吗

标签: ios objective-c uitableview radio-group


【解决方案1】:

您可以设置其他按钮的选择状态如下

-(IBAction) radioButtonsClicked:(UIButton *) sender {
    
    sender.selected = !sender.selected;
    
    for (UIView *vw in sender.superview.subviews)
    {
        if([vw isKindOfClass:[UIButton class]] && vw != sender)
        {
            UIButton *otherBtn = (UIButton*)vw;
            otherBtn.selected = NO;
        }
    }
}

用于设置按钮图像 - 开/关,在 cellforrow 中创建按钮时编写该代码..

[_btTemp setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];

[_btTemp setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];

【讨论】:

  • 好的,但是通过使用此代码,我们只能为右行选择一个按钮。但我需要的是我们只能为 Column Group 选择一个按钮,并且行有多个列组
  • 列组是什么意思?可以分享一下截图吗?
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2011-03-23
  • 2015-06-06
  • 2016-09-15
  • 2012-05-03
  • 2012-03-31
相关资源
最近更新 更多