// // YOUPAIZYSelectLabelView.m // VQU // // Created by Elaine on 2020/12/17. // Copyright © 2020 leo. All rights reserved. // #import "YOUPAIZYSelectLabelView.h" #import "UICollectionViewLeftAlignedLayout.h" #import "YOUPAILZLabelNewCell.h" @interface YOUPAIZYSelectLabelView() @property(nonatomic,strong)NSArray* youpaiplabelArray; /* */ @property (strong, nonatomic) NSMutableArray *youpaipselectArrM; @end @implementation YOUPAIZYSelectLabelView - (instancetype)initWithFrame:(CGRect)frame labelArray:(nonnull NSArray *)labelArray { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; self.youpaiplabelArray = labelArray; [self youpaifsetupView]; } return self; } -(void)youpaifsetupView{ self.youpaipselectArrM = [NSMutableArray new]; [self youpaifbuildCollectionView]; // NSLog(@"%lf", self.collectionView.collectionViewLayout.collectionViewContentSize.height); } - (void)youpaifbuildCollectionView{ _youpaipcollectionView = ({ UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init]; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout]; collectionView.backgroundColor = [UIColor clearColor]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.showsVerticalScrollIndicator = NO; //注册 [collectionView registerClass:[YOUPAILZLabelNewCell class] forCellWithReuseIdentifier:@"Cell"]; collectionView; }); [self addSubview:_youpaipcollectionView]; [_youpaipcollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.bottom.offset(0.0f); }]; } #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 { YOUPAILZLabelNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row]; @weakify(self); [cell setYoupaipdidselectBlock:^(BOOL iselect) { @strongify(self); model.youpaipisSelect = iselect; if (iselect) { [self.youpaipselectArrM addObject:model]; } else { [self.youpaipselectArrM removeObject:model]; } [self.youpaipdelegate youpaifselectLabel:self.youpaipselectArrM]; }]; [cell youpaifreloadWithModel:model]; cell.youpaipmodel = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { return [UICollectionReusableView new]; } //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示 #pragma mark - item宽高 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row]; NSString *str = model.youpaipname; CGRect rect = [str boundingRectWithSize:CGSizeMake((KScreenWidth-28.0f),20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(12)} context:nil]; return CGSizeMake(rect.size.width + 20 ,20); } #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 10.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(15,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) } //-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath //{ //// for (YOUPAIZYLabelModel *model in _labelArray) { //// model.isSelect = NO; //// } //// YOUPAIZYLabelModel *model = _labelArray[indexPath.row]; //// model.isSelect = YES; //// [collectionView reloadData]; //// [self.delegate youpaifselectLabel:model]; // //} @end