简体   繁体   English

在TableView的自定义单元格中添加单选按钮,无法访问外部

[英]Adding Radio Button in Custom Cell of TableView and unable to access outside

I am adding Radio Buttons in custom cell of UITableView and calling a method on it to changes its image or background Image. 我在UITableView的自定义单元格中添加单选按钮,并在其上调用方法来更改其图像或背景图像。 I able to set Selected button Image from sender but I want to make other buttons to their original position which I am unable to set it, as below functionality need to be look like proper Radio buttion in a cell 我能够从发件人设置选定按钮图像,但我想将其他按钮设置为原始位置,我无法设置它,因为下面的功能需要看起来像单元格中的正确无线电按钮

I am also facing problem in fetching proper Index Path in this method - (void) TableRadioButtonSelection:(id)sender; 我也遇到了在此方法中获取正确的索引路径的问题 - (void)TableRadioButtonSelection:(id)sender;

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

static NSString *RadioIdentifier = @"RadioCellIdentifier";

        RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier];
        if(myRadiocell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"RadioBtnCell" owner:self options:nil];
            myRadiocell = radioCell;

            //[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            myRadiocell.radioBtn1.tag = ButtonTag;
            //[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
             myRadiocell.radioBtn2.tag = ButtonTag1;
            //[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
             myRadiocell.radioBtn3.tag = ButtonTag2;
            myRadiocell.tag = RadioTag;

            [myRadiocell.radioBtn1 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
            [myRadiocell.radioBtn2 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];
            [myRadiocell.radioBtn3 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside];

            myRadiocell.backgroundView.backgroundColor = [UIColor clearColor];
            self.radioCell = nil;

        }

return  myRadiocell;

}

- (void) TableRadioButtonSelection:(id)sender {

    //RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview];

    RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];
    UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag];
    UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1];
    UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2];

    [mybtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
    [mybtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
    [mybtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

     //NSLog(@"%@",[cell subviews]);
    UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added.
    UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1];
    UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2];

   // I am unable to set Image of other buttons apart from sender.

    //for(UIView *item in [mysubview subviews]) {

        if ([mysubview isKindOfClass:[UIButton class]]) {
            UIButton *newBtn = (UIButton*)mysubview;
            UIButton *newBtn1 = (UIButton*)mysubview1;
            UIButton *newBtn2 = (UIButton*)mysubview2;

            NSLog(@"OK %@",newBtn);
            //[newBtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn1 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [newBtn2 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];

        }
    //}

    NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell]; 
    NSLog(@"%d",indexPath.row);

    //  Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image

    UIButton *btn = (UIButton *)sender;
    [btn setImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
    //[btn setBackgroundImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal];
}

try changing this line to this;- 尝试将此行更改为此; -

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag];

to

     RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*];

In this way you can access the table view cell.After that you can get the button from cell content views and do further coding. 通过这种方式,您可以访问表格视图单元格。之后,您可以从单元格内容视图中获取按钮并进行进一步编码。

Suppose if you do not know the indexPath then you can create the indexPath fromm the rowNumber of the table view(I think you have stored the table row number in RadioTag, if yes so you can use this field as a parameter). 假设您不知道indexPath,那么您可以从表视图的rowNumber创建indexPath(我认为您已将表行号存储在RadioTag中,如果是,则可以将此字段用作参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM