如何在表视图中创建单选按钮

时间:2015-02-19 08:41:40

标签: ios objective-c uibutton radio-button radiobuttonlist

我在表格视图单元格中有一个单选按钮。这是我的单选按钮

-(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];
} }

在上面的编码单选按钮没有变为选择状态。请帮我编码。

3 个答案:

答案 0 :(得分:4)

创建按钮时,将图像设置为选定状态和未选择状态。

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];
      }
}

答案 1 :(得分:1)

-(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;

}

答案 2 :(得分:0)

  1. 为控件事件UIControlEventValueChanged设置目标操作。
  2. 在操作发生之前(在cellForRowAtIndexPath方法中)将图像设置为特定状态。通过使用setImage:forState:您实际上不会更改当前控制图像,您只需说控件使用精确图像来处理该状态的某个控制状态。如果highlighted状态更合适,请不要使用selected状态。尝试按[radiobtn setSelected:YES][radiobtn setSelected:!radiobtn.isSelected]
  3. 以编程方式更改状态