UISwitch 突然将​​背景颜色从 clear Color 更改为 white Color

UISwitch suddenly changed background color from clearColor to whiteColor

所以我有一个 UISwitch,它的 backgroundColor 已经在 tableViewCell.m 中设置为 clearColor :

- (instancetype)initWithStyle:(UITableViewCellStyle)style 
reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
{

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithHexString:@"#333333"];

    [self initUI];
}
return self;
}

- (void)initUI {
[self addSubview:self.topLine];
[self addSubview:self.imgView];
[self addSubview:self.titleLab];
[self addSubview:self.rightView];
[self addSubview:self.rightSwitch];
[self addSubview:self.cellLine];
[self addSubview:self.bottomLine];
}

- (UISwitch *)rightSwitch {
if (!_rightSwitch) {
    self.rightSwitch = [[UISwitch alloc] init];
    self.rightSwitch.frame = CGRectMake(253*kScaleXAndWidth, 8*kScaleYAndHeight, 51*kScaleXAndWidth, 31*kScaleYAndHeight);
    self.rightSwitch.hidden = YES;
    [self.rightSwitch setBackgroundColor:[UIColor clearColor]];
    [self.rightSwitch addTarget:self action:@selector(rightSwitchClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _rightSwitch;
}

rightSwitchClick是一个块,然后在cellForRowAtIndexPath TableViewController.m :

QuickLoginCell *cell = [tableView dequeueReusableCellWithIdentifier:QuickLoginCellID forIndexPath:indexPath];
cell.rightView.hidden = YES;
cell.rightSwitch.hidden = NO;
__block QuickLoginCell *blockCell = cell;
 if (isIDlogin) {
            [cell.rightSwitch setEnabled:NO];
        }
        else{
            [cell.rightSwitch setEnabled:YES];
        }
        cell.rightSwitch.on = NO;
        cell.bottomLine.hidden = NO;
        if (![BetwayUtils isEmptyString:patternLock]) {
            cell.rightSwitch.on = YES;
            cell.bottomLine.hidden = YES;
        }

        [cell.imgView setImage:[UIImage imageNamed:@"ic_patternLock"]];
  cell.rightSwitchAddClick = ^{
            if (blockCell.rightSwitch.on) {
                PatternLockViewController *vc = [PatternLockViewController new];

                [strongSelf.navigationController pushViewController:vc animated:YES];
            }
            else{

            }
        };

所以打开的时候会直接跳到PatternLockViewController,我设置patternLock后又会跳到TableViewController,现在开关就打开了。问题是当我尝试将其关闭时,背景颜色突然变为白色,如下所示:

当我删除时:

            PatternLockViewController *vc = [PatternLockViewController new];

            [strongSelf.navigationController pushViewController:vc animated:YES]; 

所以块内没有代码,UISwitch backgroundColor 是 clearColor,我尝试打开和关闭,它按预期工作。所以我对这个问题有点困惑,因为我没有在任何地方将 UISwitch backgroundColor 设置为白色。

更新

已经尝试在从 patternlockviewcontroller 弹出时使用委托刷新 table 但仍然无效

我用 :

解决了
- (void)prepareForReuse {
[super prepareForReuse];

[self.rightSwitch setBackgroundColor:[UIColor clearColor]];
[self.rightSwitch setTintColor:[UIColor whiteColor]];
[self.rightSwitch setThumbTintColor:[UIColor whiteColor]];
}

在我的 tableViewCell.m 我希望它能帮助这里的人。