// // KBFindMiddleView.m // YouHuiProject // // Created by xiaoxi on 2018/1/19. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBFindMiddleView.h" #import "KBCollectionView.h" #import "KBFindMiddleCollectionViewCell.h" static NSString *const cellID = @"KBFindMiddleCollectionViewCell"; @interface KBFindMiddleView () @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation KBFindMiddleView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; [self initSubviews]; } return self; } - (void)initSubviews { CALayer *lineLayer = [CALayer layer]; lineLayer.backgroundColor = [UIColor YHColorWithHex:0xdddddd].CGColor; lineLayer.frame = CGRectMake(0, 0, kScreenWidth, 0.5); [self.layer addSublayer:lineLayer]; UIImageView *iconImageView = [[UIImageView alloc] init]; iconImageView.backgroundColor = [UIColor clearColor]; iconImageView.image = [UIImage imageNamed:@"goods"]; [self addSubview:iconImageView]; UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.backgroundColor = [UIColor clearColor]; nameLabel.text = @"商品分类"; nameLabel.textColor = [UIColor YHColorWithHex:0x222222]; nameLabel.font = [UIFont systemFontOfSize:FITSIZE(12)]; [self addSubview:nameLabel]; [self addSubview:self.collectionView]; [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self).offset(FITSIZE(15)); make.centerY.equalTo(nameLabel); make.size.mas_equalTo(CGSizeMake(FITSIZE(12), FITSIZE(12))); }]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iconImageView.mas_right).offset(FITSIZE(8)); make.top.equalTo(self).offset(FITSIZE(16)); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self); make.top.equalTo(self).offset(FITSIZE(28)); make.right.equalTo(self); make.bottom.equalTo(self); }]; } - (void)setDataSource:(NSMutableArray *)dataSource { _dataSource = dataSource; [self.collectionView reloadData]; } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBFindMiddleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; KBFindChannelModel *model = self.dataSource[indexPath.item]; cell.model = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(yh_FindMiddleViewDidSelectItemAtIndexPath:)]) { [self.delegate yh_FindMiddleViewDidSelectItemAtIndexPath:indexPath]; } } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; flowLayout.itemSize = CGSizeMake(kScreenWidth/4, FITSIZE(66)); flowLayout.minimumLineSpacing = FITSIZE(26); flowLayout.minimumInteritemSpacing = FITSIZE(0); flowLayout.sectionInset = UIEdgeInsetsMake(FITSIZE(26), 0, FITSIZE(16), 0); _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout]; _collectionView.scrollEnabled = NO; _collectionView.bounces = NO; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[KBFindMiddleCollectionViewCell class] forCellWithReuseIdentifier:cellID]; } return _collectionView; } @end