开发者

Add Radio Button in Tableview Got Some Problem

I know xcode don't have radio Button

so I try to add a custom button and make it action like a radio button

This is the image I use

Add Radio Button in Tableview Got Some Problem

Add Radio Button in Tableview Got Some Problem

and this is the code I set to cell

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.acces开发者_Python百科soryView= but;

and this is the problem I want to ask is

how can I give a void in - (IBAction)radioButton:(UIButton *)button

To control Two Radio Button in Two Rows

If Row 1 Radio button's selected is YES

btn in row 2 will be btn.state=NO and won't response the

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

It will be like this pic

Add Radio Button in Tableview Got Some Problem

How to set the if conditions in - (IBAction)radioButton:(UIButton *)button

this pic is fake...I only add the button in the cell...and changed the text color

Great Thanks to all stack overflow friends~


OK..here is how I add a button into a tableview : in tableviewController.h :

@interface RootViewController : UITableViewController {
    NSMutableArray *radioButtonArray;
}

@property (nonatomic ,retain)NSMutableArray *radioButtonArray;

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

and give it a (IBAction) void

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

than you can add your button into the cell

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


You will need an array of all the radio buttons. Remember that table cells get recycled/may not be visible, etc. so create an array just with the buttons and then grab the right button out of that array in your tableView:cellForIndexPath: method.

So in your tableView:cellForIndexPath: method you would do something like this:

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

Then, in your radioButton: radioButtonPressed: method you would do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜