在单独的方法中获取UITableViewCell的控件(UIButtons)及其标签

时间:2013-04-10 07:56:36

标签: iphone uitableview uibutton

我有一个UITableViewCell,其中我正在创建一些按钮(其作用类似于radiobuttons)并为按钮分配标签。但标签不是静态的。他们很有活力。现在,当点击特定的单选按钮时,我需要将该按钮设置为某个图像(radio_On.png)并将所有按钮设置为(radio_Off.png)。但是为了设置图像,我无法理解如何获取该特定单元格中所有按钮的标记值,因为它们不是静态的。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    btTemp1 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblQuestion.frame.origin.y+lblQuestion.frame.size.height+3,17, 17)];
    [btTemp1 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    btTemp1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btTemp1 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
    [btTemp1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btTemp1.titleLabel.font =[UIFont systemFontOfSize:14.f];

    btTemp1.tag=++Optionid;
    [hlcell.contentView addSubview:btTemp1];

    btTemp2 = [[UIButton alloc]initWithFrame:CGRectMake(10, lblOption1.frame.origin.y+lblOption1.frame.size.height,17, 17)];
    [btTemp2 addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    btTemp2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btTemp2 setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];
    [btTemp2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btTemp2.titleLabel.font =[UIFont systemFontOfSize:14.f];

    btTemp2.tag=++Optionid;
    [hlcell.contentView addSubview:btTemp2];
}

-(IBAction) radioButtonClicked:(UIButton *) sender {
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
    [[sender superview] superview]];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];

    [sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal];
}

当我检查TableView单元格中的其他按钮时,一个radiobuttton没有被释放。

编辑:

Prevoiusly我给了这段代码:

-(IBAction) radioButtonClicked:(UIButton *) sender {
        UIButton *button = (UIButton *)sender;
        NSLog(@"%d", [button tag]);
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
        [[sender superview] superview]];

        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];

UIButton  *btnTemp1 = (UIButton*)[cell viewWithTag:10]; //Static .But now I want dynamic
        UIButton  *btnTemp2 = (UIButton*)[cell viewWithTag:11];//Static .But now I want dynamic
        UIButton  *btnTemp3 = (UIButton*)[cell viewWithTag:12];//Static .But now I want dynamic
        UIButton *btnTemp4=(UIButton *)[cell viewWithTag:13];//Static .But now I want dynamic
        UIButton *btnTemp5=(UIButton *)[cell viewWithTag:14];//Static .But now I want dynamic

        [radioButtonsinaSection addObject:btnTemp1];
        [radioButtonsinaSection addObject:btnTemp2];
        [radioButtonsinaSection addObject:btnTemp3];
        [radioButtonsinaSection addObject:btnTemp4];
        [radioButtonsinaSection addObject:btnTemp5];


    }


    for(int i=0;i<[radioButtonsinaSection count];i++){

        [[radioButtonsinaSection objectAtIndex:i] setImage:[UIImage imageNamed:@"radio_button_off.png"] forState:UIControlStateNormal];

    }
    [sender setImage:[UIImage imageNamed:@"radio-On.png"] forState:UIControlStateNormal];
    }

如何获取单击按钮(单选按钮)的单元格的所有控件(按钮及其标签)?

1 个答案:

答案 0 :(得分:1)

根据我的知识,你错过了radioButtonClicked方法中的循环,它跟踪点击了哪个按钮..你试试这个代码..

   (IBAction) radioButtonClicked:(UIButton *) sender{

               for(int i=0;i&amp;lt;[self.radioButtons count];i++){
                       [[self.radioButtons objectAtIndex:i] setImage:[UIImage    
                                   imageNamed:@&quot;radio-off.png&quot;]  
                                        forState:UIControlStateNormal];
               }
               [sender setImage:[UIImage imageNamed:@&quot;radio-On.png&quot;]   
                                     forState:UIControlStateNormal];
}

我认为这对你有帮助,如果不能随意再问......

享受..