JLSubCollectionView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // JLSubCollectionView.m
  3. // OnlyViewControllerLink
  4. //
  5. // Created by JayLuo on 2020/4/25.
  6. // Copyright © 2020 朱伟阁. All rights reserved.
  7. //
  8. #import "JLSubCollectionView.h"
  9. #import "JLSubCell.h"
  10. #import "UILabel+Size.h"
  11. @interface JLSubCollectionView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>
  12. @property(nonatomic, strong)UICollectionView *nodeCV;
  13. @property (nonatomic,strong) NSMutableArray *dataArr;
  14. @end
  15. @implementation JLSubCollectionView
  16. static NSString * const reuseIdentifier = @"Cell";
  17. - (instancetype)initWithFrame:(CGRect)frame contentType:(NSString *)contentType selfVc:(JLSubCollectionView *)selfVc{
  18. self = [super initWithFrame:frame];
  19. if(self){
  20. _self=selfVc;
  21. _contentType=contentType;
  22. _currentPageIndex=1;
  23. _dataSource=[[NSMutableArray alloc] initWithCapacity:0];
  24. //
  25. _cv=self.nodeCV;
  26. // _cv.backgroundColor=UICOLOR_RANDOM;
  27. [self addSubview:_cv];
  28. //暂时没用上 监听回调刷新数据可以加上
  29. //[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(backLast) name:@"ReloadProdctData" object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)setModel:(JLSelectModel *)model {
  34. if (_nineAndTenStr==10) {
  35. _dataArr=[NSMutableArray arrayWithObjects:@"og1k3",@"og1k3",@"og1k3",@"sc",@"sc",@"sc",@"jlffc",@"jlffc",@"ft", nil];
  36. }else{
  37. _dataArr=[NSMutableArray arrayWithObjects:@"sc",@"og1k3",@"jlffc",@"sc",@"ft",@"jlffc",@"sc",@"K3",@"sc",@"K3", nil];
  38. }
  39. _model = model;
  40. }
  41. -(UICollectionView *)nodeCV{
  42. if(!_nodeCV){
  43. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  44. layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
  45. layout.minimumLineSpacing = 10;
  46. layout.minimumInteritemSpacing = 10;
  47. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  48. layout.itemSize = CGSizeMake((self.bounds.size.width-50)/3, 77);
  49. _nodeCV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:layout];
  50. _nodeCV.backgroundColor = [UIColor whiteColor];
  51. _nodeCV.showsHorizontalScrollIndicator = NO;
  52. _nodeCV.showsVerticalScrollIndicator=YES;
  53. _nodeCV.bounces = NO;
  54. _nodeCV.delegate = self;
  55. _nodeCV.dataSource = self;
  56. _nodeCV.showsHorizontalScrollIndicator = NO;
  57. // [_nodeCV registerClass:[JLSubCell class] forCellWithReuseIdentifier:NSStringFromClass([JLSubCell class])];
  58. [_nodeCV registerNib:[UINib nibWithNibName:NSStringFromClass([JLSubCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([JLSubCell class])];
  59. }
  60. return _nodeCV;
  61. }
  62. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  63. {
  64. return 1;
  65. }
  66. - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  67. return _model.data.count;
  68. }
  69. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  70. JLSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([JLSubCell class]) forIndexPath:indexPath];
  71. JLSelectItemModel *model = _model.data[indexPath.row];
  72. cell.label.text = [NSString stringWithFormat:@"%@", model.subName];
  73. cell.headIMGx.image=[UIImage imageNamed:_dataArr[indexPath.row]];
  74. return cell;
  75. }
  76. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. NSLog(@"%@选中了%ld个", _contentType,indexPath.row);
  79. // 回调
  80. if ([self.delegate respondsToSelector:@selector(subCollection:didSelectedItemindexPath:model:)]) {
  81. [self.delegate subCollection:self didSelectedItemindexPath:indexPath model:_model];
  82. }
  83. }
  84. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  85. JLSelectItemModel *model = _model.data[indexPath.row];
  86. CGFloat width = [UILabel getWidthWithTitle:model.subName font:[UIFont systemFontOfSize:13]];
  87. CGFloat height = [UILabel getHeightByWidth:width title:model.subName font:[UIFont systemFontOfSize:13]];
  88. return CGSizeMake((self.bounds.size.width-50)/3, 77);
  89. }
  90. @end