口袋优选

KBFindTopView.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // KBFindTopView.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/1/19.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBFindTopView.h"
  9. #import "KBCollectionView.h"
  10. #import "KBFindTopCollectionViewCell.h"
  11. static NSString *const cellID = @"KBFindTopCollectionViewCell";
  12. @interface KBFindTopView () <UICollectionViewDelegate,UICollectionViewDataSource>
  13. @property (nonatomic, strong) UICollectionView *collectionView;
  14. @end
  15. @implementation KBFindTopView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backgroundColor = [UIColor whiteColor];
  20. [self initSubviews];
  21. }
  22. return self;
  23. }
  24. - (void)initSubviews {
  25. UIImageView *iconImageView = [[UIImageView alloc] init];
  26. iconImageView.backgroundColor = [UIColor clearColor];
  27. iconImageView.image = [UIImage imageNamed:@"hot_search"];
  28. [self addSubview:iconImageView];
  29. UILabel *nameLabel = [[UILabel alloc] init];
  30. nameLabel.backgroundColor = [UIColor clearColor];
  31. nameLabel.text = @"正在热搜";
  32. nameLabel.textColor = [UIColor YHColorWithHex:0x222222];
  33. nameLabel.font = [UIFont systemFontOfSize:FITSIZE(12)];
  34. [self addSubview:nameLabel];
  35. [self addSubview:self.collectionView];
  36. [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.equalTo(self).offset(FITSIZE(15));
  38. make.centerY.equalTo(nameLabel);
  39. make.size.mas_equalTo(CGSizeMake(FITSIZE(12), FITSIZE(11)));
  40. }];
  41. [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.equalTo(iconImageView.mas_right).offset(FITSIZE(8));
  43. make.top.equalTo(self).offset(FITSIZE(16));
  44. }];
  45. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.left.equalTo(self);
  47. make.top.equalTo(self).offset(FITSIZE(44));
  48. make.size.mas_equalTo(CGSizeMake(kScreenWidth, FITSIZE(75)));
  49. }];
  50. }
  51. - (void)setDataSource:(NSMutableArray *)dataSource {
  52. _dataSource = dataSource;
  53. [self.collectionView reloadData];
  54. }
  55. #pragma mark - collectionView
  56. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  57. return 1;
  58. }
  59. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  60. return self.dataSource.count;
  61. }
  62. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  63. KBFindTopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  64. KBFindHotSearchModel *model = self.dataSource[indexPath.item];
  65. cell.model = model;
  66. return cell;
  67. }
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  69. if ([self.delegate respondsToSelector:@selector(yh_FindTopViewDidSelectItemAtIndexPath:)]) {
  70. [self.delegate yh_FindTopViewDidSelectItemAtIndexPath:indexPath];
  71. }
  72. }
  73. #pragma mark - lazy
  74. - (UICollectionView *)collectionView {
  75. if (!_collectionView) {
  76. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  77. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  78. flowLayout.itemSize = CGSizeMake(FITSIZE(108), FITSIZE(75));
  79. flowLayout.minimumLineSpacing = FITSIZE(10);
  80. flowLayout.minimumInteritemSpacing = FITSIZE(10);
  81. flowLayout.sectionInset = UIEdgeInsetsMake(0, FITSIZE(15), 0, FITSIZE(15));
  82. _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  83. _collectionView.scrollEnabled = NO;
  84. _collectionView.bounces = NO;
  85. _collectionView.showsVerticalScrollIndicator = NO;
  86. _collectionView.delegate = self;
  87. _collectionView.dataSource = self;
  88. [_collectionView registerClass:[KBFindTopCollectionViewCell class] forCellWithReuseIdentifier:cellID];
  89. }
  90. return _collectionView;
  91. }
  92. @end