Tableview 单元格内的 RadioButton iOS

RadioButton inside Tableview cell iOS

我遵循了 RadioButton.I 的 this 教程,已将其与 tableview.The 集成,问题是当我 select table 行时,单选按钮没有得到 selected。只有当我单击 RadioButton 时,它才会 selected。当 table 行为 selected.The 时,我希望单选按钮得到 selected 代码如下:

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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }
    RadioButton *rb1 = [[RadioButton alloc] initWithGroupId:@"Group" index:indexPath.row];
    rb1.frame = CGRectMake(20,13,22,22);
    [cell.contentView addSubview:rb1];
    cell.textLabel.text = @"ABC";
    return cell;
}

RadioButton with Tableview

didSelectRowAtIndexPath

上执行以下代码

Objective C

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIButton *btn = (UIButton *)[cell viewWithTag:101];
        [btn setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected];
        btn.selected = YES;
        NSLog(@"Button : %@",btn);
    }



    - (UIImage *)imageWithColor:(UIColor *)color {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return image;
    }

//在 "cellforRowAtIndexpath"

中为您的 Btn 设置标签
rb1.tag=101;

在 didSelectRowAtIndexPath 中 ->

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *btn = (UIButton*)[cell viewWithTag:101]; 
 btn.backgroundColor=[UIColor redColor];
btn.Selected = YES;
 [btn handleButtonTap:self];