YOUPAIHRSearchHeaderView.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // YOUPAIHRSearchHeaderView.m
  3. // VQU
  4. //
  5. // Created by xiaohaoran on 2021/11/9.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAIHRSearchHeaderView.h"
  9. #import "UICollectionViewLeftAlignedLayout.h"
  10. #import "YOUPAIHRSearchHeaderCell.h"
  11. @interface YOUPAIHRSearchHeaderView ()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  12. @property (nonatomic, strong) NSMutableArray *youpaiphistoryArray;
  13. @property(nonatomic,strong)UILabel *youpaipnotextL;
  14. @end
  15. @implementation YOUPAIHRSearchHeaderView
  16. - (instancetype)initWithFrame:(CGRect)frame youpaiphistoryArray:(NSMutableArray *)historyArr
  17. {
  18. if (self = [super initWithFrame:frame]) {
  19. self.youpaiphistoryArray = historyArr;
  20. [self youpaifsetSubView];
  21. }
  22. return self;
  23. }
  24. -(void)youpaifsetSubView{
  25. //历史记录
  26. UIView *youpaipbgView = [UIView new];
  27. [self addSubview:youpaipbgView];
  28. [youpaipbgView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.mas_equalTo(0);
  30. make.right.mas_equalTo(0);
  31. make.top.mas_equalTo(0);
  32. make.height.mas_equalTo(44);
  33. }];
  34. //title
  35. UILabel *titleLabel = [UILabel new];
  36. [youpaipbgView addSubview:titleLabel];
  37. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.left.mas_equalTo(14);
  39. make.centerY.mas_equalTo(0);
  40. }];
  41. titleLabel.text = @"历史记录";
  42. titleLabel.font = LCBoldFont(14);
  43. titleLabel.textColor = [UIColor blackColor];
  44. titleLabel.textAlignment = NSTextAlignmentLeft;
  45. UILabel *youpaipnotextL = [[UILabel alloc] init];
  46. [youpaipbgView addSubview:youpaipnotextL];
  47. self.youpaipnotextL = youpaipnotextL;
  48. [youpaipnotextL mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.mas_equalTo(titleLabel.mas_bottom).offset(2);
  50. make.left.mas_equalTo(titleLabel);
  51. }];
  52. youpaipnotextL.text = @"无搜索历史";
  53. youpaipnotextL.font = [UIFont systemFontOfSize:14];
  54. youpaipnotextL.textColor = [UIColor blackColor];
  55. youpaipnotextL.textAlignment = NSTextAlignmentLeft;
  56. if (self.youpaiphistoryArray.count>0) {
  57. youpaipnotextL.hidden = YES;
  58. }else{
  59. youpaipnotextL.hidden = NO;
  60. }
  61. //删除
  62. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  63. [youpaipbgView addSubview:btn];
  64. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.right.mas_equalTo(-14);
  66. make.centerY.mas_equalTo(0);
  67. make.size.mas_equalTo(CGSizeMake(28, 30));
  68. }];
  69. [btn setImage:[UIImage imageNamed:@"vqu_images_chatroom_sort_recycle"] forState:UIControlStateNormal];
  70. [btn addTarget:self action:@selector(youpaifclearnSearchHistory:) forControlEvents:UIControlEventTouchUpInside];
  71. [self youpaifbuildCollectionView];
  72. }
  73. -(void)youpaifbuildCollectionView{
  74. _youpaipcollectionView = ({
  75. //collectionView
  76. UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
  77. CGRect rect = CGRectMake(0,44,KScreenWidth,self.frame.size.height);
  78. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
  79. collectionView.backgroundColor = [UIColor clearColor];
  80. collectionView.delegate = self;
  81. collectionView.dataSource = self;
  82. collectionView.showsVerticalScrollIndicator = NO;
  83. //注册
  84. [collectionView registerClass:[YOUPAIHRSearchHeaderCell class] forCellWithReuseIdentifier:@"YOUPAIHRSearchHeaderCell"];
  85. collectionView;
  86. });
  87. [self addSubview:_youpaipcollectionView];
  88. }
  89. #pragma mark - <UICollectionViewDataSource>
  90. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  91. return 1;
  92. }
  93. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  94. if (self.youpaiphistoryArray.count>0) {
  95. NSInteger count = 0;
  96. if(self.youpaiphistoryArray.count >= 10){
  97. count = 10;
  98. }else {
  99. count = self.youpaiphistoryArray.count;
  100. }
  101. NSMutableArray *arr = [NSMutableArray new];
  102. for (int i = 0; i<count; i++) {
  103. [arr addObject:self.youpaiphistoryArray[i]];
  104. }
  105. self.youpaiphistoryArray= arr;
  106. }
  107. return self.youpaiphistoryArray.count;
  108. }
  109. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  110. YOUPAIHRSearchHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAIHRSearchHeaderCell" forIndexPath:indexPath];
  111. cell.youpaipnameStr = [NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]];
  112. return cell;
  113. }
  114. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  115. UICollectionReusableView *reusableview = nil;
  116. return reusableview;
  117. }
  118. #pragma mark - item宽高
  119. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  120. NSString *str = [NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]];
  121. CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,25) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(12)} context:nil];
  122. CGFloat width = 0;
  123. if (str.length>=20) {
  124. width = 160;
  125. }else{
  126. width = rect.size.width;
  127. }
  128. return CGSizeMake(width+30 ,25);
  129. }
  130. #pragma mark - <UICollectionViewDelegateFlowLayout>
  131. #pragma mark - X间距
  132. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  133. return 10.0;
  134. }
  135. #pragma mark - Y间距
  136. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  137. return 10.0;
  138. }
  139. //设置段落的内边距
  140. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  141. {
  142. return UIEdgeInsetsMake(10,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
  143. }
  144. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. // YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
  147. // [self.delegate youpaifclickLabel:model];
  148. if (self.youpaiptapAction) {
  149. self.youpaiptapAction([NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]]);
  150. }
  151. }
  152. -(void)youpaifclearnSearchHistory:(UIButton*)sender{
  153. [self.youpaipcollectionView removeFromSuperview];
  154. self.youpaipnotextL.hidden = NO;
  155. [_youpaiphistoryArray removeAllObjects];
  156. [NSKeyedArchiver archiveRootObject:_youpaiphistoryArray toFile:KHistorySearchPath];
  157. [[NSNotificationCenter defaultCenter]postNotificationName:@"changeHisHeaderView" object:nil];;
  158. }
  159. @end