// // KBChildHeaderView.m // YouHuiProject // // Created by 小花 on 2018/1/18. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBChildHeaderView.h" @interface KBChildHeaderView() { CGFloat _height; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSArray *dataArr; @property (nonatomic, weak) id delegate; @end @implementation KBChildHeaderView - (instancetype)initWithFrame:(CGRect)frame delegete:(id)delegate{ self = [super initWithFrame:frame]; if (self) { self.delegate = delegate; self.backgroundColor = [UIColor clearColor]; // self.layer.shadowColor = [UIColor YHColorWithHex:0x7A7979].CGColor; // self.layer.shadowOffset = CGSizeMake(2, 5); // self.layer.shadowOpacity = 0.3; // self.layer.shadowRadius = 3; } return self; } - (void)setChannelDataArr:(NSArray *)dataArr { self.dataArr = dataArr; NSInteger row = self.dataArr.count%4 == 0 ? self.dataArr.count/4:self.dataArr.count/4+1; _height = row * Fitsize(90); if (row > 0) { self.height = _height + 5; }else { self.height = _height; } [self makeUI]; } - (void)makeUI { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.itemSize = CGSizeMake((self.width)/4, Fitsize(90)); flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, _height) collectionViewLayout:flowLayout]; [self.collectionView registerClass:[YHChildTypeCell class] forCellWithReuseIdentifier:@"YHChildTypeCell"]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.scrollEnabled = NO; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.layer.cornerRadius = Fitsize(8); // self.collectionView.layer.shadowColor = [UIColor blackColor].CGColor; // self.collectionView.layer.shadowOffset = CGSizeMake(2, 5); // self.collectionView.layer.shadowOpacity = 0.3; // self.collectionView.layer.shadowRadius = 3; [self addSubview: self.collectionView]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _dataArr.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YHChildTypeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"YHChildTypeCell" forIndexPath:indexPath]; KBChildCategoryModel *model = self.dataArr[indexPath.row]; cell.model = model; return cell; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(Fitsize(10), 0, 0, 0); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildCategoryModel *model = _dataArr[indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(YHChildHeaderViewDidSelectedIndex:model:)]) { [self.delegate YHChildHeaderViewDidSelectedIndex:indexPath.row model:model]; } } @end #pragma mark ------------------ @interface YHChildTypeCell() @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *label; @end @implementation YHChildTypeCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self makeUI]; } return self; } - (void)makeUI { [self addSubview:self.imageView]; [self addSubview:self.label]; [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.mas_centerX); make.top.mas_equalTo(Fitsize(5)); make.width.height.mas_equalTo(Fitsize(48)); }]; [self.label mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(Fitsize(3)); make.left.right.mas_equalTo(0); make.height.mas_equalTo(Fitsize(15)); }]; } - (void)setModel:(KBChildCategoryModel *)model { _model = model; self.label.text = model.name; // [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil]; [self.imageView sd_setFadeImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:0 progress:nil completed:nil]; } - (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] init]; _imageView.layer.cornerRadius = Fitsize(4); _imageView.layer.masksToBounds = YES; _imageView.image = [UIImage imageNamed:@"category"]; } return _imageView; } - (UILabel *)label { if (!_label) { _label = [[UILabel alloc] init]; _label.textAlignment = NSTextAlignmentCenter; _label.font = [UIFont systemFontOfSize:12]; _label.textColor = [UIColor YHColorWithHex:0x444444]; _label.text = @"人气榜单"; } return _label; } @end