猎豆优选

LDChildHeaderView.m 4.9KB

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