如何在表格视图中一次选择单选按钮

时间:2015-02-20 08:27:05

标签: ios objective-c iphone uibutton radio

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

-(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 = [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];
        cell.accessoryView = newRadioButton;

        if ([indexPath isEqual:selectedIndex]) 
        {
            newRadioButton.selected = YES;
        } 
        else 
        {
            newRadioButton.selected = NO;
        }
    }
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndex = indexPath;
    [table reloadData];
}

-(void)radiobtn:(id)sender
{
    if([sender isSelected])
    {
        [sender setSelected:NO];
    } else
    {
        [sender setSelected:YES];
    }
}

它的工作,但我想一次只选择一个单选按钮,如此图像(左)。但是我选择了所有的单选按钮(右)。

image correct image wrong

我需要让它看起来像第一张图片。

4 个答案:

答案 0 :(得分:2)

我认为你应该尝试:

YourViewController.h中的

  @interface YourViewController : UITableViewController {
     NSIndexPath *selectedIndex
    } 
YourViewController.m中的

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
        if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        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];
        cell.accessoryView = newRadioButton;
     }
       if ([indexPath isEqual:selectedIndex]) {
         newRadioButton.seleted = YES; 
       } else {
         newRadioButton.seleted = NO;
       }
   cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
  }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        selectedIndex = indexPath;
        [tableView reloadData];
 }

link可能会帮助您

答案 1 :(得分:0)

使用以下内容取消选择if语句之前的所有单选按钮:

for (UITableViewCell *cell in [self visibleCells]) {
    [[cell accessoryView] setSelected:NO];
}

答案 2 :(得分:0)

在cellForRowAtindexpth中创建按钮时,附加一个唯一的功能,通过该功能可以识别正在点击的按钮。要做到这一点,UIButton有标签属性

你需要为cellForRowAtindexpth中的每个按钮设置标签,比如

// where i is just a static int variable for holding the count

myRadioButton.tag = i ;
 i=i+1 

现在当您根据标签点击时,您可以检查按下了哪个按钮,然后您可以选择该按钮 知道按下了哪个按钮你可以这样知道

-(IBAction)buttonPressed:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);   
}

答案 3 :(得分:0)

尝试重复使用按钮

  

解决方案:我们可以添加&从视图中按标签访问控件

     
      
  1. 如果通过标签找到控件,我们可以使用它。
  2.   
  3. 如果找不到控件,我们必须添加标签。
  4.   
UIButton *newRadioButton = (UIButton *)cell.accessoryView;
if (!newRadioButton || ![newRadioButton isKindOfClass:[UIButton class]]) {
    UIButton *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];
    cell.accessoryView = newRadioButton;
}