123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // 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()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- @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 - <UICollectionViewDataSource>
- - (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 - <UICollectionViewDelegateFlowLayout>
- #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
|