CNLiveSettingCell.m 6.05 KB
//
//  CNLiveSettingCell.m
//  CNLiveSettingModule
//
//  Created by CNLive-zxw on 2019/9/6.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveSettingCell.h"
#import <Masonry/Masonry.h>
#import "CNLiveSettingModel.h"

@interface CNLiveSettingCell()
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *content;
@property (nonatomic, strong) UIImageView *front;
@property (nonatomic, strong) UISwitch *switchOn;

@end

@implementation CNLiveSettingCell
static const NSInteger margin = 12;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];

        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.title];
        [self.contentView addSubview:self.content];
        [self.contentView addSubview:self.front];
        [self.contentView addSubview:self.switchOn];
        
    }
    return self;
}
- (void)setModel:(CNLiveSettingModel *)model{
    _model = model;
    _title.text = model.title;
    _content.text = model.content;
    [self xw_layoutSubviews];
}

- (void)xw_layoutSubviews{
    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(0);
        make.right.equalTo(self.contentView.mas_right).with.offset(0);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.height.offset(self.model.lineHeight);
        
    }];
    
    [_title mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.line.mas_left).with.offset(margin);
        make.top.equalTo(self.line.mas_bottom).with.offset(0);
        make.height.offset(self.model.height-self.model.lineHeight);
        
    }];

    [_front mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.line.mas_right).with.offset(-margin);
        make.top.equalTo(self.title.mas_top).with.offset(0);
        make.width.with.offset(margin);
        make.height.equalTo(self.title.mas_height);

    }];

    switch (self.model.type) {
        case CNLiveSettingModelTypeText:
        {
            _front.hidden = NO;
            _content.hidden = NO;
            _switchOn.hidden = YES;
            [_content mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.title.mas_top).with.offset(0);
                make.left.equalTo(self.title.mas_right).with.offset(0);
                make.right.equalTo(self.front.mas_left).with.offset(-margin);
                make.height.equalTo(self.title.mas_height);
                
            }];
        }
            break;
        case CNLiveSettingModelTypeSwitch:
        {
            _front.hidden = YES;
            _content.hidden = YES;
            _switchOn.hidden = NO;
            [_switchOn mas_updateConstraints:^(MASConstraintMaker *make) {
//                UISwithch的大小无效.默认大小 51.0f 31.0f)
                make.top.equalTo(self.line.mas_bottom).with.offset(margin);
                make.right.equalTo(self.line.mas_right).with.offset(-margin);

            }];

        }
            break;
            
        default:
        {
            _front.hidden = NO;
            _content.hidden = YES;
            _switchOn.hidden = YES;

        }
            break;
    }
    
}
- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    _line.backgroundColor = lineColor;
}
- (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
}

#pragma mark - 响应方法
- (void)switchChanged:(UISwitch *)sw{
    
}

#pragma mark - 懒加载
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
    }
    return _line;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.numberOfLines = 1;
        _title.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
        _title.font = [UIFont systemFontOfSize:16];
    }
    return _title;
}

- (UILabel *)content {
    if (!_content) {
        _content = [[UILabel alloc] init];
        _content.numberOfLines = 1;
        _content.lineBreakMode = NSLineBreakByTruncatingTail;
        _content.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
        _content.font = [UIFont systemFontOfSize:14];
    }
    return _content;
}

- (UIImageView *)front {
    if (!_front) {
        _front = [[UIImageView alloc] init];
        UIImage *xw_image = [self xw_getImageName:@"mine_info_front" bundleName:@"CNLiveSettingModule" targetClass:[CNLiveSettingCell class]];
        _front.image = xw_image;
        _front.contentMode = UIViewContentModeCenter;

    }
    return _front;
}

- (UISwitch *)switchOn{
    if(!_switchOn){
        _switchOn = [[UISwitch alloc] init];
        [_switchOn addTarget:self action:@selector(switchChanged:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _switchOn;
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
    
}

@end