UITableView中的列组只能选择一个RadioButton

时间:2016-10-21 06:27:32

标签: ios objective-c uitableview radio-group

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

我的UITableView列数为n
事情就是根据一把钥匙将柱子分组。因此,我的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);
} 

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

任何人都可以帮助我。任何帮助都会非常明显。

1 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
-(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;
        }
    }
}
&#13;
&#13;
&#13;

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

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

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

相关问题