123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // KBChildHeaderView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/18.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBChildHeaderView.h"
- @interface KBChildHeaderView()<UICollectionViewDelegate, UICollectionViewDataSource> {
- CGFloat _height;
- }
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSArray *dataArr;
- @property (nonatomic, weak) id<YHChildHeaderViewDelegate> delegate;
- @end
- @implementation KBChildHeaderView
- - (instancetype)initWithFrame:(CGRect)frame delegete:(id<YHChildHeaderViewDelegate>)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
|