new_BGMcategory.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // new_BGMcategory.m
  3. // BuguLive
  4. //
  5. // Created by bugu on 2019/5/25.
  6. // Copyright © 2019年 xfg. All rights reserved.
  7. //
  8. #import "new_BGMcategory.h"
  9. @implementation new_BGMcategory
  10. - (instancetype)init
  11. {
  12. self =[super init];
  13. return self;
  14. }
  15. - (void)setDataSource:(NSArray *)dataSource
  16. {
  17. _dataSource =dataSource;
  18. self.collectionview.frame =self.bounds;
  19. [self.collectionview reloadData];
  20. }
  21. #pragma 代理
  22. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  23. {
  24. new_bgmcategoryCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  25. cell.model =[new_bgmcategoryModel mj_objectWithKeyValues:_dataSource[indexPath.item]];
  26. return cell;
  27. }
  28. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  29. {
  30. return 1;
  31. }
  32. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  33. {
  34. return _dataSource.count;
  35. }
  36. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  37. {
  38. new_bgmcategoryModel * model =[new_bgmcategoryModel mj_objectWithKeyValues:_dataSource[indexPath.item]];
  39. if ([_delegate respondsToSelector:@selector(itemSelect:)])
  40. {
  41. [_delegate itemSelect:model];
  42. }
  43. }
  44. #pragma 懒加载
  45. - (UICollectionView *)collectionview
  46. {
  47. if (!_collectionview)
  48. {
  49. CGFloat item_w =(kScreenW-4) /4;
  50. UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
  51. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  52. layout.minimumLineSpacing =0;
  53. layout.minimumInteritemSpacing =0;
  54. layout.itemSize =CGSizeMake(item_w, item_w);
  55. // layout.headerReferenceSize =CGSizeMake(kScreenW, 470);
  56. _collectionview =[[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
  57. _collectionview.delegate =self;
  58. _collectionview.dataSource =self;
  59. [self addSubview:_collectionview];
  60. [_collectionview registerClass:[new_bgmcategoryCell class] forCellWithReuseIdentifier:@"cell"];
  61. _collectionview.scrollEnabled =NO;
  62. _collectionview.backgroundColor =kWhiteColor;
  63. UIView *fg1 =[[UIView alloc]init]
  64. ,*fg2 =[[UIView alloc]init];
  65. [self addSubview:fg1];
  66. [self addSubview:fg2];
  67. fg1.backgroundColor =UIColorFromRGB(0xe1e1e1);
  68. fg2.backgroundColor =UIColorFromRGB(0xe1e1e1);
  69. [fg1 mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.left.equalTo(self).offset(30);
  71. make.right.equalTo(self).offset(-30);
  72. make.top.equalTo(self);
  73. make.height.equalTo(@(1));
  74. }];
  75. [fg2 mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.equalTo(self).offset(30);
  77. make.right.equalTo(self).offset(-30);
  78. make.bottom.equalTo(self);
  79. make.height.equalTo(@(1));
  80. }];
  81. }
  82. return _collectionview;
  83. }
  84. /*
  85. // Only override drawRect: if you perform custom drawing.
  86. // An empty implementation adversely affects performance during animation.
  87. - (void)drawRect:(CGRect)rect {
  88. // Drawing code
  89. }
  90. */
  91. @end