我希望添加单选按钮到UITableview。单选按钮的功能应该类似于我选择单选按钮的单选按钮,该单选按钮应该被选中,而其他单元中的其他单选按钮应该被取消选中。它应该是种或者,那只有一个单选按钮可以选择。单选按钮里面的UITableview不工作
-2
A
回答
3
创建一个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];
}
+0
试试这个,这是工作正常,我 – 2014-09-03 11:12:40
+0
感谢您的答复。是的,它在didselect中运行良好。但我在单击单选按钮上寻找相同的功能。 – 2014-09-03 11:38:49
+0
好的,谢谢... :) – 2014-09-03 11:44:26
0
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];
}
添加关于你的单选按钮,小区一些代码 – raki 2014-09-03 11:11:08