2010-11-18 52 views
4

我知道的Xcode没有单选按钮添加单选按钮泰伯维了一些问题

所以我尝试添加自定义按钮,使它像一个单选按钮

行动这是我使用的图像 alt textalt text

,这是我设置到小区

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

在BTN行2将btn.state=NO并且不会响应

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

这将是这样的PIC alt text

如何设置,如果在- (IBAction)radioButton:(UIButton *)button

的这张照片是假的条件......我只添加单元格中的按钮...并改变文字颜色

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

+0

你知道的TableView可以管理本地检查单元格:http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewStyles/TableViewCharacteristics。html#// apple_ref/doc/uid/TP40007451-CH3-SW4 – 2010-11-18 10:25:41

+0

我知道......但是我的老板坚持这样看起来更好......所以你知道...... – 2010-11-18 10:34:36

回答

0

您将需要一个所有单选按钮的数组。请记住,表格单元格被回收/可能不可见等,因此只需使用按钮创建一个数组,然后在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]; 
    } 
} 
+0

哦!这是一个很好的解决方案〜第一次我想我使用2个单元格,所以我只需要如果&else,使用数组比我的方式好得多!如何取消无法点击的行? – 2010-11-18 14:02:50

1

OK..here是我怎么加按钮放入tableview: in tableviewController.h:

@interface RootViewController : UITableViewController { 
    NSMutableArray *radioButtonArray; 
} 

@property (nonatomic ,retain)NSMutableArray *radioButtonArray; 

在tableviewController.hm

- (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为)无效

- (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; 
} 
+0

如果没有选中按钮,仍然不知道如何让单元格不能点击? – 2010-11-19 16:48:09

+0

Thanks.It工程.. – kushalrshah 2014-03-29 07:19:41