在提出这个问题之前,我搜索了很多但没有找到合适的答案。
我的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);
}
所以我可以在所有列中选择单选按钮,但实际上我想要的是 对于每个列组,只能选择一个单选按钮 。 />
任何人都可以帮助我。任何帮助都会非常明显。
答案 0 :(得分:0)
您可以将其他按钮的选择状态设置为以下
-(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;
用于设置按钮图像 - 开/关,在cellforrow中创建按钮时写入该代码..
[_btTemp setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[_btTemp setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
&#13;