// // KBNineNineHeaderView.m // YouHuiProject // // Created by xiaoxi on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBNineNineHeaderView.h" #import "KBCollectionView.h" #import "KBNineNineHeaderCollectionViewCell.h" #import "WSLWaterFlowLayout.h" static NSString *const cellID = @"KBNineNineHeaderCollectionViewCell"; @interface KBNineNineHeaderView () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableDictionary *cellIDDict; @end @implementation KBNineNineHeaderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; self.backgroundColor = sex == 0 ? [UIColor YHColorWithHex:0xff8daa]: [UIColor YHColorWithHex:0x98befd]; [self initSubviews]; } return self; } - (void)initSubviews { [self addSubview:self.collectionView]; } - (void)setHeaderModel:(YHNineNineHeaderModel *)headerModel { _headerModel = headerModel; [self.collectionView reloadData]; } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 0) { return self.headerModel.first.count; } else { return self.headerModel.last.count; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = [self.cellIDDict objectForKey:[NSString stringWithFormat:@"%@", indexPath]]; if (identifier == nil) { identifier = [NSString stringWithFormat:@"%@%@", NSStringFromClass([KBNineNineHeaderCollectionViewCell class]), [NSString stringWithFormat:@"%@", indexPath]]; [self.cellIDDict setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]]; [self.collectionView registerClass:[KBNineNineHeaderCollectionViewCell class] forCellWithReuseIdentifier:identifier]; } KBNineNineHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; if (indexPath.section == 0) { YHNineNineHeaderCollectionModel *model = self.headerModel.first[indexPath.item]; cell.model = model; } else { YHNineNineHeaderCollectionModel *model = self.headerModel.last[indexPath.item]; cell.model = model; } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { YHNineNineHeaderCollectionModel *model = nil; if (indexPath.section == 0) { model = self.headerModel.first[indexPath.item]; } else { model = self.headerModel.last[indexPath.item]; } if ([self.delegate respondsToSelector:@selector(yh_NineNineHeaderViewDidSelectItem:)]) { [self.delegate yh_NineNineHeaderViewDidSelectItem:model]; } } #pragma mark - flowLayout - (CGFloat)rowCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 2; } - (CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return FITSIZE(4); } - (CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return FITSIZE(4); } - (UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return UIEdgeInsetsMake(FITSIZE(10), FITSIZE(15), FITSIZE(15), FITSIZE(15)); } - (CGFloat)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath itemHeight:(CGFloat)itemHeight { if (indexPath.section == 0) { YHNineNineHeaderCollectionModel *model = self.headerModel.first[indexPath.item]; if ([model.type isEqual:@2]) { return FITSIZE(171); } else { return FITSIZE(83.5); } } else { YHNineNineHeaderCollectionModel *model = self.headerModel.last[indexPath.item]; if ([model.type isEqual:@2]) { return FITSIZE(171); } else { return FITSIZE(83.5); } } } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { WSLWaterFlowLayout *flowLayout = [[WSLWaterFlowLayout alloc] init]; flowLayout.delegate = self; flowLayout.flowLayoutStyle = WSLHorizontalWaterFlow; _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight, self.width, self.height-NavBarHeight) collectionViewLayout:flowLayout]; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.delegate = self; _collectionView.dataSource = self; } return _collectionView; } - (NSMutableDictionary *)cellIDDict { if (!_cellIDDict) { _cellIDDict = [NSMutableDictionary dictionary]; } return _cellIDDict; } @end