// // JLSubCollectionView.m // OnlyViewControllerLink // // Created by JayLuo on 2020/4/25. // Copyright © 2020 朱伟阁. All rights reserved. // #import "JLSubCollectionView.h" #import "JLSubCell.h" #import "UILabel+Size.h" @interface JLSubCollectionView () @property(nonatomic, strong)UICollectionView *nodeCV; @property (nonatomic,strong) NSMutableArray *dataArr; @end @implementation JLSubCollectionView static NSString * const reuseIdentifier = @"Cell"; - (instancetype)initWithFrame:(CGRect)frame contentType:(NSString *)contentType selfVc:(JLSubCollectionView *)selfVc{ self = [super initWithFrame:frame]; if(self){ _self=selfVc; _contentType=contentType; _currentPageIndex=1; _dataSource=[[NSMutableArray alloc] initWithCapacity:0]; // _cv=self.nodeCV; // _cv.backgroundColor=UICOLOR_RANDOM; [self addSubview:_cv]; //暂时没用上 监听回调刷新数据可以加上 //[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(backLast) name:@"ReloadProdctData" object:nil]; } return self; } - (void)setModel:(JLSelectModel *)model { if (_nineAndTenStr==10) { _dataArr=[NSMutableArray arrayWithObjects:@"og1k3",@"og1k3",@"og1k3",@"sc",@"sc",@"sc",@"jlffc",@"jlffc",@"ft", nil]; }else{ _dataArr=[NSMutableArray arrayWithObjects:@"sc",@"og1k3",@"jlffc",@"sc",@"ft",@"jlffc",@"sc",@"K3",@"sc",@"K3", nil]; } _model = model; } -(UICollectionView *)nodeCV{ if(!_nodeCV){ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.itemSize = CGSizeMake((self.bounds.size.width-50)/3, 77); _nodeCV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:layout]; _nodeCV.backgroundColor = [UIColor whiteColor]; _nodeCV.showsHorizontalScrollIndicator = NO; _nodeCV.showsVerticalScrollIndicator=YES; _nodeCV.bounces = NO; _nodeCV.delegate = self; _nodeCV.dataSource = self; _nodeCV.showsHorizontalScrollIndicator = NO; // [_nodeCV registerClass:[JLSubCell class] forCellWithReuseIdentifier:NSStringFromClass([JLSubCell class])]; [_nodeCV registerNib:[UINib nibWithNibName:NSStringFromClass([JLSubCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([JLSubCell class])]; } return _nodeCV; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _model.data.count; } - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { JLSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([JLSubCell class]) forIndexPath:indexPath]; JLSelectItemModel *model = _model.data[indexPath.row]; cell.label.text = [NSString stringWithFormat:@"%@", model.subName]; cell.headIMGx.image=[UIImage imageNamed:_dataArr[indexPath.row]]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%@选中了%ld个", _contentType,indexPath.row); // 回调 if ([self.delegate respondsToSelector:@selector(subCollection:didSelectedItemindexPath:model:)]) { [self.delegate subCollection:self didSelectedItemindexPath:indexPath model:_model]; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { JLSelectItemModel *model = _model.data[indexPath.row]; CGFloat width = [UILabel getWidthWithTitle:model.subName font:[UIFont systemFontOfSize:13]]; CGFloat height = [UILabel getHeightByWidth:width title:model.subName font:[UIFont systemFontOfSize:13]]; return CGSizeMake((self.bounds.size.width-50)/3, 77); } @end