// // HAndTSwitchTableViewCell.m // LekangGuard // // Created by ecell on 2022/11/25. // #import "HAndTSwitchTableViewCell.h" @interface HAndTSwitchTableViewCell () /// 标题 @property (nonatomic ,weak) UILabel *titleLabel; /// 开关 @property (nonatomic ,weak) UISwitch *customSwitch; @end @implementation HAndTSwitchTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = KKWhiteColorColor; [self subCellView]; } return self; } - (void)setTitleStr:(NSString *)titleStr { _titleStr = titleStr; self.titleLabel.text = titleStr; } - (void)setIsSwitchOpen:(BOOL)isSwitchOpen { _isSwitchOpen = isSwitchOpen; self.customSwitch.on = isSwitchOpen; } - (void)subCellView { UILabel *titleLabel = [UICommon ui_label:CGRectZero lines:0 align:NSTextAlignmentRight font:FontADA_(15) textColor:KKTextBlackColor text:@"" Radius:0]; self.titleLabel = titleLabel; [self.contentView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(15); make.centerY.equalTo(self.contentView); }]; UISwitch *customSwitch = [[UISwitch alloc] init]; [customSwitch setOnTintColor:KKMainColor]; [customSwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged]; self.customSwitch = customSwitch; [self.contentView addSubview:customSwitch]; [customSwitch mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView.mas_right).inset(15); make.centerY.equalTo(self.contentView); //make.size.mas_equalTo(CGSizeMake(Adapted(80), Adapted(40))); }]; } /** * 按钮切换事件监听回调方法 */ - (void) switchChange:(UISwitch*)sw { if(sw.on == YES) { NSLog(@"开关切换为开"); } else if(sw.on == NO) { NSLog(@"开关切换为关"); } self.isSwitch(sw.on); } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end