天天省钱快报

KBModuleView.m 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // KBModuleView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/4.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBModuleView.h"
  9. #import "KBModuleCollectionCell.h"
  10. static NSString *ldModuleCollectionCell = @"ldModuleCollectionCell";
  11. @interface KBModuleView()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
  12. NSArray *_dataArr;
  13. }
  14. @property (nonatomic, strong) UICollectionView *collectionView;
  15. @end
  16. @implementation KBModuleView
  17. - (void)setRecommonData:(NSArray *)dataArr {
  18. _dataArr = dataArr.mutableCopy;
  19. [self.collectionView reloadData];
  20. }
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.layer.masksToBounds = YES;
  25. [self initSubView];
  26. }
  27. return self;
  28. }
  29. - (void)initSubView {
  30. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  31. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
  32. flowLayout.minimumLineSpacing = 5;
  33. flowLayout.minimumInteritemSpacing = 10;
  34. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  35. self.collectionView.backgroundColor = [UIColor whiteColor];
  36. [self.collectionView registerClass:[KBModuleCollectionCell class] forCellWithReuseIdentifier:ldModuleCollectionCell];
  37. self.collectionView.delegate = self;
  38. self.collectionView.dataSource = self;
  39. self.collectionView.showsHorizontalScrollIndicator = NO;
  40. [self addSubview:self.collectionView];
  41. }
  42. #pragma mark ---- UICollectionView Delegate ------
  43. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  44. return CGSizeMake((SCREEN_WIDTH-30)/3, self.collectionView.height);
  45. }
  46. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  47. return 1;
  48. }
  49. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  50. return _dataArr.count;
  51. }
  52. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  53. {
  54. return CGSizeMake(0, 0);
  55. }
  56. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  57. {
  58. return CGSizeMake(0, 0);
  59. }
  60. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  61. KBChildGoodModel *model = _dataArr[indexPath.row];
  62. KBModuleCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:ldModuleCollectionCell forIndexPath:indexPath];
  63. cell.model = model;
  64. return cell;
  65. }
  66. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  67. return UIEdgeInsetsMake(0, 10, 0, 0);
  68. }
  69. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  70. KBChildGoodModel *model = _dataArr[indexPath.row];
  71. if (self.didSelectedGood) {
  72. self.didSelectedGood(model);
  73. }
  74. }
  75. @end