天天省钱快报

KBNineNineHeaderView.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // KBNineNineHeaderView.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBNineNineHeaderView.h"
  9. #import "KBCollectionView.h"
  10. #import "KBNineNineHeaderCollectionViewCell.h"
  11. #import "WSLWaterFlowLayout.h"
  12. static NSString *const cellID = @"KBNineNineHeaderCollectionViewCell";
  13. @interface KBNineNineHeaderView () <UICollectionViewDelegate,UICollectionViewDataSource,WSLWaterFlowLayoutDelegate>
  14. @property (nonatomic, strong) UICollectionView *collectionView;
  15. @property (nonatomic, strong) NSMutableDictionary *cellIDDict;
  16. @end
  17. @implementation KBNineNineHeaderView
  18. - (instancetype)initWithFrame:(CGRect)frame {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue];
  22. self.backgroundColor = sex == 0 ? [UIColor YHColorWithHex:0xff8daa]: [UIColor YHColorWithHex:0x98befd];
  23. [self initSubviews];
  24. }
  25. return self;
  26. }
  27. - (void)initSubviews {
  28. [self addSubview:self.collectionView];
  29. }
  30. - (void)setHeaderModel:(YHNineNineHeaderModel *)headerModel {
  31. _headerModel = headerModel;
  32. [self.collectionView reloadData];
  33. }
  34. #pragma mark - collectionView
  35. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  36. return 2;
  37. }
  38. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  39. if (section == 0) {
  40. return self.headerModel.first.count;
  41. }
  42. else {
  43. return self.headerModel.last.count;
  44. }
  45. }
  46. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  47. NSString *identifier = [self.cellIDDict objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
  48. if (identifier == nil) {
  49. identifier = [NSString stringWithFormat:@"%@%@", NSStringFromClass([KBNineNineHeaderCollectionViewCell class]), [NSString stringWithFormat:@"%@", indexPath]];
  50. [self.cellIDDict setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
  51. [self.collectionView registerClass:[KBNineNineHeaderCollectionViewCell class] forCellWithReuseIdentifier:identifier];
  52. }
  53. KBNineNineHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  54. if (indexPath.section == 0) {
  55. YHNineNineHeaderCollectionModel *model = self.headerModel.first[indexPath.item];
  56. cell.model = model;
  57. }
  58. else {
  59. YHNineNineHeaderCollectionModel *model = self.headerModel.last[indexPath.item];
  60. cell.model = model;
  61. }
  62. return cell;
  63. }
  64. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  65. YHNineNineHeaderCollectionModel *model = nil;
  66. if (indexPath.section == 0) {
  67. model = self.headerModel.first[indexPath.item];
  68. }
  69. else {
  70. model = self.headerModel.last[indexPath.item];
  71. }
  72. if ([self.delegate respondsToSelector:@selector(yh_NineNineHeaderViewDidSelectItem:)]) {
  73. [self.delegate yh_NineNineHeaderViewDidSelectItem:model];
  74. }
  75. }
  76. #pragma mark - flowLayout
  77. - (CGFloat)rowCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
  78. return 2;
  79. }
  80. - (CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
  81. return FITSIZE(4);
  82. }
  83. - (CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
  84. return FITSIZE(4);
  85. }
  86. - (UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout {
  87. return UIEdgeInsetsMake(FITSIZE(10), FITSIZE(15), FITSIZE(15), FITSIZE(15));
  88. }
  89. - (CGFloat)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath itemHeight:(CGFloat)itemHeight {
  90. if (indexPath.section == 0) {
  91. YHNineNineHeaderCollectionModel *model = self.headerModel.first[indexPath.item];
  92. if ([model.type isEqual:@2]) {
  93. return FITSIZE(171);
  94. }
  95. else {
  96. return FITSIZE(83.5);
  97. }
  98. }
  99. else {
  100. YHNineNineHeaderCollectionModel *model = self.headerModel.last[indexPath.item];
  101. if ([model.type isEqual:@2]) {
  102. return FITSIZE(171);
  103. }
  104. else {
  105. return FITSIZE(83.5);
  106. }
  107. }
  108. }
  109. #pragma mark - lazy
  110. - (UICollectionView *)collectionView {
  111. if (!_collectionView) {
  112. WSLWaterFlowLayout *flowLayout = [[WSLWaterFlowLayout alloc] init];
  113. flowLayout.delegate = self;
  114. flowLayout.flowLayoutStyle = WSLHorizontalWaterFlow;
  115. _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight, self.width, self.height-NavBarHeight) collectionViewLayout:flowLayout];
  116. _collectionView.showsHorizontalScrollIndicator = NO;
  117. _collectionView.delegate = self;
  118. _collectionView.dataSource = self;
  119. }
  120. return _collectionView;
  121. }
  122. - (NSMutableDictionary *)cellIDDict {
  123. if (!_cellIDDict) {
  124. _cellIDDict = [NSMutableDictionary dictionary];
  125. }
  126. return _cellIDDict;
  127. }
  128. @end