【问题标题】:How to select radio button at a time in table view如何在表格视图中一次选择单选按钮
【发布时间】:2015-04-21 20:51:41
【问题描述】:

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

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

它的工作,但我想像这张图片(左)一样一次只选择一个单选按钮。但对我来说选择所有单选按钮(右)。

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

【问题讨论】:

    标签: ios objective-c iphone uibutton radio


    【解决方案1】:

    我觉得你应该试试:

    在 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 可以帮助你

    【讨论】:

    • @GowthamK 我知道为什么。您不再需要为 newRadioButton 添加选择器 remove -(void)radiobtn:(id)sender 和 [newRadioButton addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
    • 我已经删除了那个兄弟,但现在单选按钮没有改变
    • @GowthamK 看到我的编辑。用我的替换你的 cellForRowAtIndexPath: 代码
    • 我再次更改了您编辑的代码,仍然无法正常工作,为什么?
    • 看我的代码没有 cell.selectionStyle = UITableViewCellSelectionStyleNone;如果 ([indexPath isEqual:selectedIndex]) 不在 if(cell == nil) {}
    【解决方案2】:

    取消选择 if 语句之前的所有单选按钮:

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

    【讨论】:

    • 在您的 if 语句之前的 radiobtn 方法中。
    【解决方案3】:

    当您在 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]);   
    }
    

    【讨论】:

      【解决方案4】:

      尝试重复使用按钮

      解决方案:我们可以从视图中通过标签添加和访问控制

      1. 如果通过标签找到控件,我们可以使用它。
      2. 如果没有找到控件,我们必须用标签添加它。
      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;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-17
        • 2017-07-10
        • 2014-03-13
        • 2018-09-19
        • 2013-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-19
        相关资源
        最近更新 更多