CNLiveUserInfoCell.m 6.25 KB
//
//  CNLiveUserInfoCell.m
//  CNLiveBMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveUserInfoCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "CNLiveUserInfoModel.h"
#import "CNLiveCategory.h"

@interface CNLiveUserInfoCell()
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UILabel *content;
@property (nonatomic, strong) UIImageView *front;
@property (nonatomic, strong) UIImageView *pic;

@end

@implementation CNLiveUserInfoCell
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.pic];
        
    }
    return self;
}

- (void)setImage:(UIImage *)image{
    _image = image;
    if(self.model.dataType == CNLiveUserInfoDataTypeHeader){
        _pic.image = image;
    }
}

- (void)setModel:(CNLiveUserInfoModel *)model{
    _model = model;
    _title.text = model.title;
    _content.text = model.info;
    
    if(model.dataType == CNLiveUserInfoDataTypeHeader){
        UIImage *xw_image = [self getImageWithImageName:@"default_avatar_y" bundleName:@"CNLiveBMineModule" targetClass:[self class]];
        [_pic sd_setImageWithURL:[NSURL URLWithString:model.info] placeholderImage:xw_image];

    }else if (model.dataType == CNLiveUserInfoDataTypeNickname){
        

    }else if (model.dataType == CNLiveUserInfoDataTypeGender){
        if([model.info isEqualToString:@"m"]){
            _content.text = @"男";
        }else if([model.info isEqualToString:@"f"]){
            _content.text = @"女";
        }else{
            _content.text = @"未知";
        }
    }
    
    [self xw_layoutSubviews];
}

- (void)xw_layoutSubviews{
    
    CGFloat line_margin = self.model.lineHeight >= 5?0:margin;
    
    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(line_margin);
        make.right.equalTo(self.contentView.mas_right).with.offset(-line_margin);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(0);
        make.height.offset(self.model.lineHeight);
        
    }];
    
    [_title mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.height.offset(self.model.height-self.model.lineHeight);
        
    }];
    
    [_front mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.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.layoutType) {
        case CNLiveUserInfoLayoutTypeText:
        {
            _content.hidden = NO;
            _pic.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(margin);
                make.right.equalTo(self.front.mas_left).with.offset(-margin);
                make.height.equalTo(self.title.mas_height);
            }];
        }
            break;
        case CNLiveUserInfoLayoutTypeImage:
        {
            CGFloat height = self.model.height-self.model.lineHeight-2*margin;
            _content.hidden = YES;
            _pic.hidden = NO;
            [_pic mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView.mas_top).with.offset(margin);
                make.right.equalTo(self.front.mas_left).with.offset(-margin);
                make.width.height.offset(height);
            }];
            _pic.contentMode = UIViewContentModeScaleToFill;
            _pic.layer.masksToBounds = YES;
            _pic.layer.cornerRadius = height/2.0;
        }
            break;
            
        default:
        {
            _content.hidden = YES;
            _pic.hidden = YES;
            
        }
            break;
    }
    
}
- (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 - 懒加载
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/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];
        _content.textAlignment = NSTextAlignmentRight;
    }
    return _content;
}

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

    }
    return _front;
}
- (UIImageView *)pic {
    if (!_pic) {
        _pic = [[UIImageView alloc] init];
    }
    return _pic;
}
#pragma mark - 响应方法
- (void)switchChanged:(UISwitch *)sw{
    
}

@end