CNLiveNewsUpPicTableViewCell.m 5.58 KB
//
//  CNLiveNewsUpPicTableViewCell.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/21.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveNewsUpPicTableViewCell.h"
#import "CNHomeModel.h"

@interface CNLiveNewsUpPicTableViewCell()
@property (nonatomic, strong) UIImageView *backgroundImage;//背景图
@property (nonatomic, strong) UIImageView *picImage;//一张图片
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *playImage;//播放
@property (nonatomic, strong) UILabel *projectLabel;//专题

@end

@implementation CNLiveNewsUpPicTableViewCell
//static const NSInteger margin = 10;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = HEXCOLOR(0xebebeb);
        self.contentView.layer.masksToBounds = YES;

        _backgroundImage = [[UIImageView alloc] init];
        _backgroundImage.contentMode = UIViewContentModeCenter;
        _backgroundImage.layer.masksToBounds = YES;
        _backgroundImage.layer.cornerRadius = 2.0;
        _backgroundImage.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_backgroundImage];
        
        _picImage = [[UIImageView alloc] init];
        _picImage.contentMode = UIViewContentModeScaleAspectFill;
        _picImage.layer.masksToBounds = YES;
        _picImage.image = [UIImage imageNamed:@"placeholder"];
        [self.contentView addSubview:_picImage];
        
        _playImage = [[UIImageView alloc] init];
        _playImage.layer.masksToBounds = YES;
        _playImage.image = [UIImage imageNamed:@"cnlive_play"];
        [self.contentView addSubview:_playImage];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.numberOfLines = 2;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _titleLabel.textColor = HEXCOLOR(0x333333);
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_titleLabel];
        
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.numberOfLines = 1;
        _timeLabel.textColor = HEXCOLOR(0xbbbbbb);
        _timeLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_timeLabel];
        
        _projectLabel = [[UILabel alloc] init];
        _projectLabel.numberOfLines = 1;
        _projectLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _projectLabel.textColor = [UIColor whiteColor];
        _projectLabel.backgroundColor = Color_Blue;
        _projectLabel.font = [UIFont systemFontOfSize:12];
        _projectLabel.layer.masksToBounds = YES;
        _projectLabel.layer.cornerRadius = 2;
        [self.contentView addSubview:_projectLabel];
        
    }
    return self;
    
}
- (void)setModel:(CNHomeModel *)model{
    _model = model;
    _titleLabel.text = model.title;
    _timeLabel.text = [model.createDate componentsSeparatedByString:@" "][0];
    _projectLabel.text = @"";
    if(model.tags && ![model.tags isEqualToString:@""]){
        _projectLabel.text = model.tags;

    }else if(model.subjecttag && ![model.subjecttag isEqualToString:@""]){
        _projectLabel.text = model.subjecttag;
        
    }
    [_picImage sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"placeholder"]];

    [self layoutSubviews];
    
}
- (void)layoutSubviews{
    [_backgroundImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.top.equalTo(self.mas_top).with.offset(5);
        make.bottom.equalTo(self.mas_bottom).with.offset(-5);
    }];
    
    [_picImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_backgroundImage.mas_left).with.offset(5);
        make.top.equalTo(self->_backgroundImage.mas_top).with.offset(5);
        make.right.equalTo(self->_backgroundImage.mas_right).with.offset(-5);
        make.height.offset(200*MainHScale);
        
    }];
    
    [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_backgroundImage.mas_left).with.offset(10);
        make.top.equalTo(self->_picImage.mas_bottom).with.offset(5);
        make.right.equalTo(self->_backgroundImage.mas_right).with.offset(-10);
        
    }];
    
    [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_backgroundImage.mas_left).with.offset(10);
        make.right.equalTo(self->_backgroundImage.mas_right).with.offset(-10);
        make.bottom.equalTo(self->_backgroundImage.mas_bottom).with.offset(-5);
    }];
   
    if ([_model.type isEqualToString:@"program"]) {
        _playImage.hidden = NO;
        [_playImage mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->_picImage).offset(5);
            make.bottom.equalTo(self->_picImage).offset(-5);
            make.width.offset(50);
            make.height.offset(50);

        }];
        
    }
    else if ([_model.type isEqualToString:@"subject"]) {
        _playImage.hidden = YES;
        
    }
    else if ([_model.type isEqualToString:@"article"]) {
        _playImage.hidden = YES;
        
    }
    
    [_projectLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_backgroundImage.mas_right).with.offset(-10);
        make.bottom.equalTo(self->_backgroundImage.mas_bottom).with.offset(-5);
    }];
}

@end