【发布时间】:2016-10-21 06:27:32
【问题描述】:
在问这个问题之前,我搜索了很多,但找不到合适的答案。
我有一个 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);
}
所以我可以选择所有列中的单选按钮,但实际上我想要的是对于每个列组,只有一个单选按钮是可选的。
谁能帮帮我。任何帮助都将不胜感激。
【问题讨论】:
-
No.. 对我来说按钮选择/取消选择工作正常,但我想说的是,如果我的 columnGroup 之一有三个子列,所以我在每个子单元格中创建了按钮,但我只想选择一个按钮在该列组下
-
在您的单元格中,您有一组单选按钮,对吗?并且您只想允许对单个单元格进行单选
-
我认为您还需要在 radioButtonsClicked 事件中保持其他按钮状态(选中、取消选中)。
-
@rohit Sidpara 你能举个例子吗
标签: ios objective-c uitableview radio-group