口袋优选

KBChildHeaderView.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // KBChildHeaderView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/18.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBChildHeaderView.h"
  9. @interface KBChildHeaderView()<UICollectionViewDelegate, UICollectionViewDataSource> {
  10. CGFloat _height;
  11. }
  12. @property (nonatomic, strong) UICollectionView *collectionView;
  13. @property (nonatomic, strong) NSArray *dataArr;
  14. @property (nonatomic, weak) id<YHChildHeaderViewDelegate> delegate;
  15. @end
  16. @implementation KBChildHeaderView
  17. - (instancetype)initWithFrame:(CGRect)frame delegete:(id<YHChildHeaderViewDelegate>)delegate{
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. self.delegate = delegate;
  21. self.backgroundColor = [UIColor clearColor];
  22. // self.layer.shadowColor = [UIColor YHColorWithHex:0x7A7979].CGColor;
  23. // self.layer.shadowOffset = CGSizeMake(2, 5);
  24. // self.layer.shadowOpacity = 0.3;
  25. // self.layer.shadowRadius = 3;
  26. }
  27. return self;
  28. }
  29. - (void)setChannelDataArr:(NSArray *)dataArr {
  30. self.dataArr = dataArr;
  31. NSInteger row = self.dataArr.count%4 == 0 ? self.dataArr.count/4:self.dataArr.count/4+1;
  32. _height = row * Fitsize(90);
  33. if (row > 0) {
  34. self.height = _height + 5;
  35. }else {
  36. self.height = _height;
  37. }
  38. [self makeUI];
  39. }
  40. - (void)makeUI {
  41. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  42. flowLayout.itemSize = CGSizeMake((self.width)/4, Fitsize(90));
  43. flowLayout.minimumLineSpacing = 0;
  44. flowLayout.minimumInteritemSpacing = 0;
  45. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  46. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, _height) collectionViewLayout:flowLayout];
  47. [self.collectionView registerClass:[YHChildTypeCell class] forCellWithReuseIdentifier:@"YHChildTypeCell"];
  48. self.collectionView.backgroundColor = [UIColor whiteColor];
  49. self.collectionView.scrollEnabled = NO;
  50. self.collectionView.showsVerticalScrollIndicator = NO;
  51. self.collectionView.delegate = self;
  52. self.collectionView.dataSource = self;
  53. self.collectionView.layer.cornerRadius = Fitsize(8);
  54. // self.collectionView.layer.shadowColor = [UIColor blackColor].CGColor;
  55. // self.collectionView.layer.shadowOffset = CGSizeMake(2, 5);
  56. // self.collectionView.layer.shadowOpacity = 0.3;
  57. // self.collectionView.layer.shadowRadius = 3;
  58. [self addSubview: self.collectionView];
  59. }
  60. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  61. {
  62. return _dataArr.count;
  63. }
  64. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. YHChildTypeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"YHChildTypeCell" forIndexPath:indexPath];
  67. KBChildCategoryModel *model = self.dataArr[indexPath.row];
  68. cell.model = model;
  69. return cell;
  70. }
  71. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  72. {
  73. return UIEdgeInsetsMake(Fitsize(10), 0, 0, 0);
  74. }
  75. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  76. KBChildCategoryModel *model = _dataArr[indexPath.row];
  77. if (self.delegate && [self.delegate respondsToSelector:@selector(YHChildHeaderViewDidSelectedIndex:model:)]) {
  78. [self.delegate YHChildHeaderViewDidSelectedIndex:indexPath.row model:model];
  79. }
  80. }
  81. @end
  82. #pragma mark ------------------
  83. @interface YHChildTypeCell()
  84. @property (nonatomic, strong) UIImageView *imageView;
  85. @property (nonatomic, strong) UILabel *label;
  86. @end
  87. @implementation YHChildTypeCell
  88. - (instancetype)initWithFrame:(CGRect)frame {
  89. self = [super initWithFrame:frame];
  90. if (self) {
  91. [self makeUI];
  92. }
  93. return self;
  94. }
  95. - (void)makeUI {
  96. [self addSubview:self.imageView];
  97. [self addSubview:self.label];
  98. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.centerX.mas_equalTo(self.mas_centerX);
  100. make.top.mas_equalTo(Fitsize(5));
  101. make.width.height.mas_equalTo(Fitsize(48));
  102. }];
  103. [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(Fitsize(3));
  105. make.left.right.mas_equalTo(0);
  106. make.height.mas_equalTo(Fitsize(15));
  107. }];
  108. }
  109. - (void)setModel:(KBChildCategoryModel *)model {
  110. _model = model;
  111. self.label.text = model.name;
  112. // [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil];
  113. [self.imageView sd_setFadeImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:0 progress:nil completed:nil];
  114. }
  115. - (UIImageView *)imageView {
  116. if (!_imageView) {
  117. _imageView = [[UIImageView alloc] init];
  118. _imageView.layer.cornerRadius = Fitsize(4);
  119. _imageView.layer.masksToBounds = YES;
  120. _imageView.image = [UIImage imageNamed:@"category"];
  121. }
  122. return _imageView;
  123. }
  124. - (UILabel *)label {
  125. if (!_label) {
  126. _label = [[UILabel alloc] init];
  127. _label.textAlignment = NSTextAlignmentCenter;
  128. _label.font = [UIFont systemFontOfSize:12];
  129. _label.textColor = [UIColor YHColorWithHex:0x444444];
  130. _label.text = @"人气榜单";
  131. }
  132. return _label;
  133. }
  134. @end