123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- //
- // 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 () <UICollectionViewDelegate,UICollectionViewDataSource>
- @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
|