0

Before asking this question I googled a lot but not able to find suitable answer.

I'm have a UITableView with n number of Columns.
The thing is the Column is Grouped according to a key. So my UITableView header is splitted into two, one for the column group and and this column group is divided to show columns.
I have created a custom class for the cell there I display radio button in each column.

Code:

In cellForRowAtIndexPath method:

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

}

Radio button click action method;

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

So I'm able to make selections for radio buttons in all column but actually what I want is For each column group only one radio button is selectable.

Can anybody help me. Any help would be greatly appreciable.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
Jestin Saji Chacko
  • 113
  • 1
  • 1
  • 9

1 Answers1

0

You can set other button's selection status as following

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

for setting button Image - on/off, write that code at the time of creating button in cellforrow..

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

[_btTemp setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
PlusInfosys
  • 3,416
  • 1
  • 19
  • 33
  • Ok but by using this code we can only select a single button for a row right. but what i need is we can select only a single button for a **Column Group** and row has more than one column group – Jestin Saji Chacko Oct 21 '16 at 07:34
  • what do you mean by Column group? Can you please share screenshot ? – PlusInfosys Oct 21 '16 at 14:10