口袋优选

KBFindMiddleView.m 4.2KB

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