// // YOUPAILZHomeTagLabelView.m // VQU // // Created by 肖浩然的mac on 2021/8/16. // Copyright © 2021 leo. All rights reserved. // #import "YOUPAILZHomeTagLabelView.h" #import "UICollectionViewLeftAlignedLayout.h" #import "YOUPAILZHomeTagLabelCell.h" #import "YOUPAILZlaber_listModel.h" @interface YOUPAILZHomeTagLabelView() @property(nonatomic,strong)NSArray* youpaiplabelArray; @property(nonatomic,strong)NSString* youpaiptype; @property(nonatomic,strong)UICollectionView *youpaipcollectionView; @end @implementation YOUPAILZHomeTagLabelView - (instancetype)initWithFrame:(CGRect)frame LabelArr:(NSArray*)labelarr{ if (self = [super initWithFrame:frame]){ [self youpaifsetSubView]; _youpaiplabelArray = labelarr; } return self; } -(void)setYoupaiplabArr:(NSArray *)labArr{ _youpaiplabArr = labArr; self.youpaiplabelArray = labArr; [self.youpaipcollectionView reloadData]; } -(void)youpaifsetSubView{ UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init]; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; [self addSubview:collectionView]; self.youpaipcollectionView = collectionView; [collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0); }]; collectionView.backgroundColor = [UIColor clearColor]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.showsVerticalScrollIndicator = NO; //注册 [collectionView registerClass:[YOUPAILZHomeTagLabelCell class] forCellWithReuseIdentifier:@"YOUPAILZHomeTagLabelCell"]; } #pragma mark - - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _youpaiplabelArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAILZHomeTagLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAILZHomeTagLabelCell" forIndexPath:indexPath]; YOUPAILZlaber_listModel *model = _youpaiplabelArray[indexPath.row]; [cell youpaifreloadWithModel:model]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; return reusableview; } //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示 #pragma mark - item宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAILZlaber_listModel *model = _youpaiplabelArray[indexPath.row]; NSString *str = [NSString stringWithFormat:@"%@%ld",model.youpaipname,model.youpaipnum]; CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(10)} context:nil]; return CGSizeMake(rect.size.width+15 ,15); } #pragma mark - head宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeZero; } #pragma mark - foot宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeZero; } #pragma mark - #pragma mark - X间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 3.0; } #pragma mark - Y间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10.0; } //设置段落的内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0,3,0,0);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) } @end