UITableview 内的单选按钮不起作用 [英] Radio button inside the UITableview not working

查看:72
本文介绍了UITableview 内的单选按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望向 UITableview 添加单选按钮.单选按钮的功能应该就像我在表格单元格中选择单选按钮时应该选择单选按钮而其他单元格中的其他所有单选按钮都应该取消选择一样.它应该是一种或一种,即只有一个单选按钮可以选择.

I wish to add radio button to UITableview. The function of radio button should be like when I select radio button in the table cell that radio button should be selected and other all radio buttons in the other cell should be unselected. It should be kind of either or, that is only one radio button can select.

推荐答案

创建一个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];

  }

这篇关于UITableview 内的单选按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆