// // KBMorePicCollectionCell.m // YouHuiProject // // Created by jcymac on 2018/4/24. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBMorePicCollectionCell.h" #import "KBMorePicCollectionViewCell.h" #import "KBMorePicCollectionModel.h" @interface KBMorePicCollectionCell() @property(nonatomic,strong)UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArr; @end @implementation KBMorePicCollectionCell static NSInteger sumCell=0; - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; self.clipsToBounds = YES; } return self; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setModelDatas:(NSArray *)dataArr { self.dataArr = [NSMutableArray arrayWithArray:dataArr]; sumCell=self.dataArr.count; _collectionView=nil; for (UIView *view in self.subviews) { if ([view isKindOfClass:[UICollectionView class]]) { [view removeFromSuperview]; } } [self addSubview:self.collectionView]; // [self.collectionView reloadData]; } #pragma mark -UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if (self.dataArr==nil||self.dataArr.count==0) { return 0; } return self.dataArr.count; } - ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ KBMorePicCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"picCollectionViewCell" forIndexPath:indexPath]; if (!cell) { } KBMorePicCollectionModel *model=self.dataArr[indexPath.row]; cell.titleLabel.text=model.name; // [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@""]]; [cell.picImageView sd_setFadeImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:0 progress:nil completed:nil]; return cell; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 0; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.delegate && [self.delegate respondsToSelector:@selector(YHMorePicCollectionCellDidSelectedItem:)]) { [self.delegate YHMorePicCollectionCellDidSelectedItem:indexPath.row]; } } - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; if (sumCell==0) { sumCell=4; } NSInteger offHeight2=0; if (iPhone5) { offHeight2=10; } NSInteger Crow=1; NSInteger Coff=-1; NSInteger CrowItemSum=5; Coff=sumCell%CrowItemSum; if (Coff!=0) { CrowItemSum=4; Coff=sumCell%CrowItemSum; if (Coff!=0) { Coff=sumCell%3; CrowItemSum=3; if (Coff!=0) { CrowItemSum=5; } } } Crow=sumCell/CrowItemSum; if (Coff!=0) { Crow++; } float width=(SCREEN_WIDTH-FITSIZE(20))/1.0f; flowLayout.itemSize = CGSizeMake((width-4)/CrowItemSum, SCREEN_WIDTH/4+offHeight2); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(FITSIZE(10), 0, width, (SCREEN_WIDTH/4+offHeight2)*Crow) collectionViewLayout:flowLayout]; _collectionView.layer.cornerRadius=8; _collectionView.layer.masksToBounds=YES; _collectionView.backgroundColor=[UIColor whiteColor]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerNib:[UINib nibWithNibName:@"KBMorePicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"picCollectionViewCell"]; } return _collectionView; } @end