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