ios - 如何在表格 View 中一次选择单选按钮

标签 ios objective-c iphone uibutton radio-button

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

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

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

最佳答案

我认为你应该尝试:

在 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可能对你有帮助

关于ios - 如何在表格 View 中一次选择单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624528/

相关文章:

ios - UIStackView 无法满足的自动布局约束

ios - iPad 中的 Split View

ios - cocoa touch 类和普通 swift 类有什么区别?

objective-c - 使用 alloc init 而不是 new

ios - UINavigationBar barButtonItem 更改返回按钮图像和标题

iphone - 关于发行证书过期的问题?

ios - CBConcreteCentralManager 未开机错误

ios - 为多个状态设置外观 - 有没有办法用更少的行来做到这一点?

iphone 平铺图像

iphone - 使用echoprint对整个iPhone音乐库进行指纹识别