如何在 table 视图中创建单选按钮
How to create a radio button in table view
我在 table 视图单元格中有一个单选按钮。这是我的单选按钮
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 0, 15, 14.5);
[radiobtn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
[radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
[sender setSelected:YES];
} }
在上面的代码中,单选按钮没有变为选中状态。请帮我编码。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
[newRadioButton addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = newRadioButton;
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- 为控件事件 UIControlEventValueChanged 设置目标操作。
- 在动作发生之前设置特定状态的图像(在
cellForRowAtIndexPath
方法中)。通过使用 setImage:forState:
你实际上并没有改变当前的控件图像,你只是说控件使用确切的图像来表示该状态发生的某些控件状态。不要使用 highlighted
状态,以防 selected
状态更合适。尝试通过 [radiobtn setSelected:YES]
或 [radiobtn setSelected:!radiobtn.isSelected]
. 以编程方式更改状态
在创建按钮时设置选中和未选中状态的图像。
radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 0, 15, 14.5);
[radiobtn setImage:[UIImage imageNamed:@"unselect"]] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:@"select"]] forState:UIControlStateSelected];
radiobtn.tag=1;
[radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
单击按钮时,您只需更改选定状态即可。
-(void)radiobtn:(UIButton *)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
}
else
{
[sender setSelected:YES];
}
}
我在 table 视图单元格中有一个单选按钮。这是我的单选按钮
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 0, 15, 14.5);
[radiobtn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
[radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)radiobtn:(id)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
} else
[sender setSelected:YES];
} }
在上面的代码中,单选按钮没有变为选中状态。请帮我编码。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *newRadioButton;
newRadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
newRadioButton.frame = CGRectMake(30, 0, 15, 14.5);
[newRadioButton setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
[newRadioButton setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
[newRadioButton addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = newRadioButton;
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
- 为控件事件 UIControlEventValueChanged 设置目标操作。
- 在动作发生之前设置特定状态的图像(在
cellForRowAtIndexPath
方法中)。通过使用setImage:forState:
你实际上并没有改变当前的控件图像,你只是说控件使用确切的图像来表示该状态发生的某些控件状态。不要使用highlighted
状态,以防selected
状态更合适。尝试通过[radiobtn setSelected:YES]
或[radiobtn setSelected:!radiobtn.isSelected]
. 以编程方式更改状态
在创建按钮时设置选中和未选中状态的图像。
radiobtn = [UIButton buttonWithType:UIButtonTypeCustom];
radiobtn.frame = CGRectMake(30, 0, 15, 14.5);
[radiobtn setImage:[UIImage imageNamed:@"unselect"]] forState:UIControlStateNormal];
[radiobtn setImage:[UIImage imageNamed:@"select"]] forState:UIControlStateSelected];
radiobtn.tag=1;
[radiobtn addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = radiobtn;
单击按钮时,您只需更改选定状态即可。
-(void)radiobtn:(UIButton *)sender
{
if([sender isSelected])
{
[sender setSelected:NO];
}
else
{
[sender setSelected:YES];
}
}