【问题标题】:Add Radio Button in Tableview Got Some Problem在 Tableview 中添加单选按钮有一些问题
【发布时间】:2010-11-18 10:16:58
【问题描述】:

我知道 xcode 没有单选按钮

所以我尝试添加一个自定义按钮并使其像单选按钮一样操作

这是我使用的图像

这是我设置到单元格的代码

UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
[but setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
[but setFrame:CGRectMake(0, 0, 44, 44)];
[but addTarget:self action:@selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView= but;

而这就是我要问的问题是

我怎样才能在- (IBAction)radioButton:(UIButton *)button中给一个空白

控制两行中的两个单选按钮

如果第 1 行单选按钮被选中是 YES

第 2 行中的 btn 将是 btn.state=NO 并且不会响应

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

会是这样的图片

- (IBAction)radioButton:(UIButton *)button中如何设置if条件

这张图片是假的...我只在单元格中添加了按钮...并更改了文本颜色

非常感谢所有堆栈溢出的朋友~

【问题讨论】:

标签: iphone cocoa-touch uitableview radio-button


【解决方案1】:

好的..这是我将按钮添加到 tableview 中的方法: 在 tableviewController.h 中:

@interface RootViewController : UITableViewController {
    NSMutableArray *radioButtonArray;
}

@property (nonatomic ,retain)NSMutableArray *radioButtonArray;

在tableviewController.h.m中

- (void)viewDidAppear:(BOOL)animated {
    radioButtonArray = [NSMutableArray new];
    for (int i = 0; i < 30; i ++) {
        UIButton *radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [radioButton setImage:[UIImage imageNamed:@"radio-off.png"] forState:UIControlStateNormal];
        [radioButton setImage:[UIImage imageNamed:@"radio-on.png"] forState:UIControlStateSelected];
        [radioButton setFrame:CGRectMake(0, 0, 44, 44)];
        [radioButton addTarget:self action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [radioButtonArray addObject:radioButton];
    }
    [super viewDidAppear:animated];
}

并给它一个 (IBAction) void

- (IBAction)radioButtonPressed:(UIButton *)button{
    [button setSelected:YES];
    // Unselect all others.
    for (UIButton *other in radioButtonArray) {
        if (other != button) {
            other.selected=NO;
        }
    }
}

您可以将按钮添加到单元格中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryView = [radioButtonArray objectAtIndex:[indexPath row]];  
    // Configure the cell.
    return cell;
}

【讨论】:

  • 还是不知道如何让不选中按钮的单元格不能被点击?
【解决方案2】:

您将需要一个包含所有单选按钮的数组。请记住,表格单元格会被回收/可能不可见等,因此只需使用按钮创建一个数组,然后在您的 tableView:cellForIndexPath: 方法中从该数组中获取右侧按钮。

所以在你的tableView:cellForIndexPath: 方法中你会做这样的事情:

cell.accessoryView = [myButtonArray objectAtIndex:[indexPath row]];

然后,在你的 radioButton: radioButtonPressed: 方法中你会这样做:

// Select the pressed button.
[button setSelected:YES];
// Unselect all others.
for (UIButton *other in myButtonArray) {
    if (other != button) {
       [other setSelected:NO];
    }
}

【讨论】:

  • 哦!这是一个很好的解决方案〜第一次我认为我使用2个单元格,所以我只需要if&else,使用数组比我的方式好得多!如何取消未选择按钮时无法点击的行?
猜你喜欢
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2016-12-26
  • 2021-05-22
  • 2013-12-19
  • 1970-01-01
相关资源
最近更新 更多