CNLiveMineCell.m 12.4 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"
#import "CNLiveMineButtonCell.h"

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

@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) NSMutableArray *buttons;

@property (nonatomic, strong) UICollectionView *collectionView;

@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 createSubviews];
    }
    return self;
}

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

- (void)setModel:(CNLiveMineModel *)model{
    _model = model;
    
    _allButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10*RATIO, 0, 10*RATIO);
    _allButton.imageEdgeInsets = UIEdgeInsetsMake(0, 50*RATIO, 0, -50*RATIO);
    
    [_titleButton setTitle:model.name forState:UIControlStateNormal];
    _titleButton.titleLabel.font = UIFontBoldCNMake(15.0);

    UIImage *xw_image = [self getImageWithImageName:model.pic bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
    [_titleButton setImage:xw_image forState:UIControlStateNormal];
    
    _allButton.hidden = YES;
    _allButton.titleLabel.font = UIFontCNMake(12.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);
        
    }];
    _bgView.layer.masksToBounds = NO;
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOffset = CGSizeMake(0, 4);   //0,0围绕阴影四周  0,4向下有4个像素的偏移
    _bgView.layer.shadowOpacity = 0.1;   //设置阴影透明度
    _bgView.layer.shadowRadius = 5;      //设置阴影圆角
    
    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;
    }
    [_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);
        
    }];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom);
        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.bottom.equalTo(self.bgView.mas_bottom);
    }];
    [_collectionView reloadData];

}

- (void)order_layoutSubviews{
    [_titleButton setImage:nil forState:UIControlStateNormal];
    _titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//使图片和文字水平居中显示
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top);
        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(self.model.titleHeight);
        
    }];
    
    _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(self.model.titleHeight);

    }];
    
}
- (void)tool_layoutSubviews{
    [_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(self.model.titleHeight);
        
    }];
    
}
- (void)goods_layoutSubviews{
    [_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin, 0, 0)];
    [_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin, 0, 0)];
    
    _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(self.model.titleHeight);
        
    }];
    
}

- (void)sports_layoutSubviews{
    [_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin, 0, 0)];
    [_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin, 0, 0)];

    _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(self.model.titleHeight);
        
    }];
    
}

- (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)allDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:didSelectItemAtItem:)]) {
        [self.delegate cell:self didSelectItemAtItem:0];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (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 = UIFontBoldCNMake(15.0);
        [_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 = UIFontCNMake(12.0);
        [_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*RATIO, 0, 10*RATIO);
        _allButton.imageEdgeInsets = UIEdgeInsetsMake(0, 50*RATIO, 0, -50*RATIO);
                                                     
    }
    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;
}

- (UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //设置CollectionView的属性
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-2*self.model.margin, self.model.height-(50+self.model.margin)) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[CNLiveMineButtonCell class] forCellWithReuseIdentifier:kCNLiveMineButtonCell];
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (self.model.type) {
        case CNLiveMineCellTypeOrder:
        {
            return CGSizeMake((SCREEN_WIDTH-4*self.model.margin-4*self.model.margin)/5.0, self.model.itemHeight);
        }
            break;

        case CNLiveMineCellTypeTool:
        {
            return CGSizeMake((SCREEN_WIDTH-4*self.model.margin-3*self.model.margin)/4.0, self.model.itemHeight);
        }
            break;

        case CNLiveMineCellTypeGoods:
        {
            return CGSizeMake(SCREEN_WIDTH-4*self.model.margin, self.model.itemHeight);
        }
            break;

        case CNLiveMineCellTypeSports:
        {
            return CGSizeMake((SCREEN_WIDTH-4*self.model.margin-3*self.model.margin)/4.0, self.model.itemHeight);
        }
            break;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(self.model.margin, 0, self.model.margin, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.buttons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveMineButtonCell *cell = (CNLiveMineButtonCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCNLiveMineButtonCell forIndexPath:indexPath];
    cell.model = self.model.buttons[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:didSelectItemAtItem:)]) {
        [self.delegate cell:self didSelectItemAtItem:indexPath.item];
    }
}

@end