UITableview 内的单选按钮不起作用
Posted
技术标签:
【中文标题】UITableview 内的单选按钮不起作用【英文标题】:Radio button inside the UITableview not working 【发布时间】:2014-09-03 11:06:55 【问题描述】:我希望将单选按钮添加到 UITableview。单选按钮的功能应该就像我在表格单元格中选择单选按钮时应该选择单选按钮,而其他单元格中的其他所有单选按钮都应该取消选择。它应该是或者,只有一个单选按钮可以选择。
【问题讨论】:
添加一些关于您的单选按钮和单元格的代码 【参考方案1】:创建一个int
值
int selectstr;
UIButton * button;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 14.0, 215.0, 36.0)];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = NSTextAlignmentLeft;
title.textColor=[UIColor blackColor];
title.tag=indexPath.row;
title.text = @"TEST";
// title.font = [UIFont fontWithName:@"Arial Hebrew" size:16.0f];
title.font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
[cell.contentView addSubview:title];
button = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 46.0f, 30.0f)];
button.tag=indexPath.row;
if (selectstr ==indexPath.row)
[button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor colorWithRed:(243.0/255.0) green:(114.0/255.0) blue:(74.0/255.0) alpha:1.0f];
else
[button setImage:[UIImage imageNamed:@"radio_unselected.png"] forState:UIControlStateNormal];
cell.backgroundColor=[UIColor clearColor];
button.adjustsImageWhenHighlighted=NO;
[button addTarget:self action:@selector(toggleCheckedMode:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
selectstr=indexPath.row;
[tableView reloadData];
- (IBAction)toggleCheckedMode:(UIButton*)sender
[button setImage:[UIImage imageNamed:@"radio_selected.png"] forState:UIControlStateNormal];
// here customized for your self
selectstr=sender.tag;
[tableView reloadData];
【讨论】:
感谢您的回复。是的,它在 didselect 中运行良好。但我正在点击单选按钮中寻找相同的功能。 我自己修改了代码,请看代码,如果您需要帮助,我希望与您合作 它运行良好,正如我们预期的那样。谢谢 karthik jpa 然后投票给我的答案兄弟,你认识我吗,如果是的话 karthik.saral@gmail.com(Gmail、Skype、FB、堆栈溢出), 你也是兄弟,祝你今天快乐【参考方案2】:Write this code into
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSIndexPath *selectedIndex;
UIButton *radioButton;
radioButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
radioButton.frame = CGRectMake(30,23,24,24);
// [radioButton setTitle:@"" forState:UIControlStateNormal];
radioButton.tag = 8888;
[cell.contentView addSubview:radioButton];
radioButton = (UIButton *)[cell.contentView viewWithTag:8888];
if (selectedIndex && selectedIndex.row == indexPath.row)
[radioButton setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
else
[radioButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
selectedIndex =indexPath;
[self.userTabelView reloadData];
【讨论】:
以上是关于UITableview 内的单选按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章