CNLiveMineCell.m 13.8 KB
//
//  CNLiveMineCell.m
//  CNLiveMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveMineCell.h"
#import <Masonry/Masonry.h>
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveBaseKit.h"

@interface CNLiveMineCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *titleButton;
@property (nonatomic, strong) UIButton *allButton;

@property (nonatomic, strong) UIView *line;

@end

@implementation CNLiveMineCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
        [self xw_layoutSubviews];
    }
    return self;
}

- (void)xw_layoutSubviews{
    [self.contentView addSubview:self.bgView];
    [self.contentView addSubview:self.titleButton];
    [self.contentView addSubview:self.allButton];
    [self.contentView addSubview:self.line];
    
}

- (void)setModel:(CNLiveMineModel *)model{
    if(_model == model){
        return;
    }
    _model = model;
    [_titleButton setTitle:model.name forState:UIControlStateNormal];
    UIImage *xw_image = [self getImageWithImageName:model.pic bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
    [_titleButton setImage:xw_image forState:UIControlStateNormal];
    _allButton.hidden = YES;

    switch (model.type) {
        case CNLiveMineCellTypeOrder:
        {
            [self order_layoutSubviews];
        }
            break;
            
        case CNLiveMineCellTypeTool:
        {
            [self tool_layoutSubviews];
        }
            break;
            
        case CNLiveMineCellTypeGoods:
        {
            [self goods_layoutSubviews];
        }
            break;
            
        case CNLiveMineCellTypeSports:
        {
            [self sports_layoutSubviews];
        }
            break;
    }
}

- (void)order_layoutSubviews{
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
        
    }];
    [_titleButton setImage:nil forState:UIControlStateNormal];
    _titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//使图片和文字水平居中显示
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(50);
        
    }];
    
    _allButton.hidden = NO;
    [_allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleButton.mas_top);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(50);

    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(0.5);
        
    }];

    CGFloat width = SCREEN_WIDTH-2*self.model.margin;
    CGFloat btnWidth = width/self.model.titles.count*1.0;
    CGFloat btnHeight = 60;
    
    for(int i = 0; i < self.model.titles.count; i++){
        UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*i, 50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
        [self addSubview:btn];
        
    }
    
}
- (void)tool_layoutSubviews{
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
        
    }];
    [_titleButton setImage:nil forState:UIControlStateNormal];
    _titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//使图片和文字水平居中显示
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(50);
        
    }];
    _allButton.hidden = YES;

    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(0.5);
        
    }];

    CGFloat width = SCREEN_WIDTH-2*self.model.margin;
    
    CGFloat btnWidth = width/4.0;
    CGFloat btnHeight = 60;
    
    for(int i = 0; i < self.model.titles.count; i++){
        NSInteger row = i % 4;
        NSInteger col = floorf(i / 4.0);
        UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*row, (col*(btnHeight+self.model.margin))+50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
        [self addSubview:btn];

    }
}
- (void)goods_layoutSubviews{
    [_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin, 0, 0)];
    [_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin, 0, 0)];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
        
    }];
    _titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(50);
        
    }];
    _allButton.hidden = YES;

    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(0.5);
        
    }];
    CGFloat width = SCREEN_WIDTH-2*self.model.margin;
    CGFloat btnWidth = width/4.0;
    CGFloat btnHeight = 80;
    
    // 电子票
    UIButton *btn = [self createButtonWithFrame:CGRectMake((width-btnWidth)/2.0, self.model.margin+50, btnWidth, btnHeight) title:self.model.titles[0] image:self.model.images[0] tag:10000];
    [self addSubview:btn];
    
}
- (void)sports_layoutSubviews{
    [_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin, 0, 0)];
    [_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin, 0, 0)];

    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
    
    }];
    _titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(50);
        
    }];
    _allButton.hidden = YES;

    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
        make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
        make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
        make.height.with.offset(0.5);
        
    }];
    
    CGFloat width = SCREEN_WIDTH-2*self.model.margin;
    
    CGFloat btnWidth = width/4.0;
    CGFloat btnHeight = 60;
    
    for(int i = 0; i < self.model.titles.count; i++){
        NSInteger row = i % 4;
        NSInteger col = floorf(i / 4.0);
        UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*row, (col*(btnHeight+self.model.margin))+50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
        [self addSubview:btn];
        
    }
}

- (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)btnDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:clickedButtonWithType:tag:)]) {
        [self.delegate cell:self clickedButtonWithType:self.model.type tag:btn.tag];
    }
}

- (void)allDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:clickedButtonWithType:tag:)]) {
        [self.delegate cell:self clickedButtonWithType:CNLiveMineCellTypeOrder tag:10000];
    }
    
}

#pragma mark - 懒加载
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 5;
    }
    return _bgView;
}
- (UIButton *)titleButton{
    if (!_titleButton) {
        _titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_titleButton setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateNormal];
        _titleButton.adjustsImageWhenHighlighted = NO;
    }
    return _titleButton;
    
}

- (UIButton *)allButton{
    if(!_allButton){
        _allButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_allButton setTitle:@"查看全部" forState:UIControlStateNormal];
        UIImage *xw_image = [self getImageWithImageName:@"mine_all_front" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
        [_allButton setImage:xw_image forState:UIControlStateNormal];
        [_allButton setImage:xw_image forState:UIControlStateHighlighted];
        _allButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_allButton setTitleColor:[UIColor colorWithRed:190.0/255.0 green:190.0/255.0 blue:190.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        _allButton.adjustsImageWhenHighlighted = NO;
        [_allButton addTarget:self action:@selector(allDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        _allButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
        _allButton.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, -50);
                                                     
    }
    return _allButton;
}

- (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;
}

#pragma mark - 私有方法
- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title image:(NSString *)image tag:(NSInteger)tag{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = tag;
    btn.frame = frame;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIImage *xw_image = [self getImageWithImageName:image bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
    [btn setImage:xw_image forState:UIControlStateNormal];
    btn.adjustsImageWhenHighlighted = NO;
    [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleEdgeInsets = UIEdgeInsetsMake(btn.imageView.frame.size.height+self.model.margin, -btn.imageView.frame.size.width, 0, 0);
    btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.bounds.size.height, 0, self.model.margin, -btn.titleLabel.bounds.size.width);
    return btn;
    
}

@end