YOUPAIZYSelectLabelView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // YOUPAIZYSelectLabelView.m
  3. // VQU
  4. //
  5. // Created by Elaine on 2020/12/17.
  6. // Copyright © 2020 leo. All rights reserved.
  7. //
  8. #import "YOUPAIZYSelectLabelView.h"
  9. #import "UICollectionViewLeftAlignedLayout.h"
  10. #import "YOUPAILZLabelNewCell.h"
  11. @interface YOUPAIZYSelectLabelView()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  12. @property(nonatomic,strong)NSArray* youpaiplabelArray;
  13. /* */
  14. @property (strong, nonatomic) NSMutableArray *youpaipselectArrM;
  15. @end
  16. @implementation YOUPAIZYSelectLabelView
  17. - (instancetype)initWithFrame:(CGRect)frame labelArray:(nonnull NSArray *)labelArray {
  18. if (self = [super initWithFrame:frame]) {
  19. self.backgroundColor = [UIColor clearColor];
  20. self.youpaiplabelArray = labelArray;
  21. [self youpaifsetupView];
  22. }
  23. return self;
  24. }
  25. -(void)youpaifsetupView{
  26. self.youpaipselectArrM = [NSMutableArray new];
  27. [self youpaifbuildCollectionView];
  28. // NSLog(@"%lf", self.collectionView.collectionViewLayout.collectionViewContentSize.height);
  29. }
  30. - (void)youpaifbuildCollectionView{
  31. _youpaipcollectionView = ({
  32. UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
  33. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  34. collectionView.backgroundColor = [UIColor clearColor];
  35. collectionView.delegate = self;
  36. collectionView.dataSource = self;
  37. collectionView.showsVerticalScrollIndicator = NO;
  38. //注册
  39. [collectionView registerClass:[YOUPAILZLabelNewCell class] forCellWithReuseIdentifier:@"Cell"];
  40. collectionView;
  41. });
  42. [self addSubview:_youpaipcollectionView];
  43. [_youpaipcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.left.top.right.bottom.offset(0.0f);
  45. }];
  46. }
  47. #pragma mark - <UICollectionViewDataSource>
  48. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  49. return 1;
  50. }
  51. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  52. return _youpaiplabelArray.count;
  53. }
  54. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  55. YOUPAILZLabelNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  56. YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  57. @weakify(self);
  58. [cell setYoupaipdidselectBlock:^(BOOL iselect) {
  59. @strongify(self);
  60. model.youpaipisSelect = iselect;
  61. if (iselect) {
  62. [self.youpaipselectArrM addObject:model];
  63. } else {
  64. [self.youpaipselectArrM removeObject:model];
  65. }
  66. [self.youpaipdelegate youpaifselectLabel:self.youpaipselectArrM];
  67. }];
  68. [cell youpaifreloadWithModel:model];
  69. cell.youpaipmodel = model;
  70. return cell;
  71. }
  72. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  73. return [UICollectionReusableView new];
  74. }
  75. //这里我为了直观的看出每组的CGSize设置用if 后续我会用简洁的三元表示
  76. #pragma mark - item宽高
  77. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  78. YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  79. NSString *str = model.youpaipname;
  80. CGRect rect = [str boundingRectWithSize:CGSizeMake((KScreenWidth-28.0f),20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(12)} context:nil];
  81. return CGSizeMake(rect.size.width + 20 ,20);
  82. }
  83. #pragma mark - head宽高
  84. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  85. return CGSizeZero;
  86. }
  87. #pragma mark - foot宽高
  88. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  89. return CGSizeZero;
  90. }
  91. #pragma mark - <UICollectionViewDelegateFlowLayout>
  92. #pragma mark - X间距
  93. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  94. return 10.0;
  95. }
  96. #pragma mark - Y间距
  97. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  98. return 10.0;
  99. }
  100. //设置段落的内边距
  101. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  102. {
  103. return UIEdgeInsetsMake(15,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  104. }
  105. //-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  106. //{
  107. //// for (YOUPAIZYLabelModel *model in _labelArray) {
  108. //// model.isSelect = NO;
  109. //// }
  110. //// YOUPAIZYLabelModel *model = _labelArray[indexPath.row];
  111. //// model.isSelect = YES;
  112. //// [collectionView reloadData];
  113. //// [self.delegate youpaifselectLabel:model];
  114. //
  115. //}
  116. @end