123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // YOUPAIHRSearchHeaderView.m
- // VQU
- //
- // Created by xiaohaoran on 2021/11/9.
- // Copyright © 2021 MS. All rights reserved.
- //
- #import "YOUPAIHRSearchHeaderView.h"
- #import "UICollectionViewLeftAlignedLayout.h"
- #import "YOUPAIHRSearchHeaderCell.h"
- @interface YOUPAIHRSearchHeaderView ()<UICollectionViewDelegate , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
- @property (nonatomic, strong) NSMutableArray *youpaiphistoryArray;
- @property(nonatomic,strong)UILabel *youpaipnotextL;
- @end
- @implementation YOUPAIHRSearchHeaderView
- - (instancetype)initWithFrame:(CGRect)frame youpaiphistoryArray:(NSMutableArray *)historyArr
- {
- if (self = [super initWithFrame:frame]) {
- self.youpaiphistoryArray = historyArr;
-
- [self youpaifsetSubView];
- }
- return self;
- }
- -(void)youpaifsetSubView{
- //历史记录
- UIView *youpaipbgView = [UIView new];
- [self addSubview:youpaipbgView];
- [youpaipbgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(0);
- make.right.mas_equalTo(0);
- make.top.mas_equalTo(0);
- make.height.mas_equalTo(44);
- }];
-
- //title
- UILabel *titleLabel = [UILabel new];
- [youpaipbgView addSubview:titleLabel];
- [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(14);
- make.centerY.mas_equalTo(0);
- }];
- titleLabel.text = @"历史记录";
- titleLabel.font = LCBoldFont(14);
- titleLabel.textColor = [UIColor blackColor];
- titleLabel.textAlignment = NSTextAlignmentLeft;
- UILabel *youpaipnotextL = [[UILabel alloc] init];
- [youpaipbgView addSubview:youpaipnotextL];
- self.youpaipnotextL = youpaipnotextL;
- [youpaipnotextL mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(titleLabel.mas_bottom).offset(2);
- make.left.mas_equalTo(titleLabel);
- }];
-
- youpaipnotextL.text = @"无搜索历史";
- youpaipnotextL.font = [UIFont systemFontOfSize:14];
- youpaipnotextL.textColor = [UIColor blackColor];
- youpaipnotextL.textAlignment = NSTextAlignmentLeft;
- if (self.youpaiphistoryArray.count>0) {
- youpaipnotextL.hidden = YES;
- }else{
- youpaipnotextL.hidden = NO;
- }
- //删除
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [youpaipbgView addSubview:btn];
- [btn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.mas_equalTo(-14);
- make.centerY.mas_equalTo(0);
- make.size.mas_equalTo(CGSizeMake(28, 30));
- }];
- [btn setImage:[UIImage imageNamed:@"vqu_images_chatroom_sort_recycle"] forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(youpaifclearnSearchHistory:) forControlEvents:UIControlEventTouchUpInside];
-
- [self youpaifbuildCollectionView];
-
-
-
- }
- -(void)youpaifbuildCollectionView{
-
- _youpaipcollectionView = ({
- //collectionView
- UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
- CGRect rect = CGRectMake(0,44,KScreenWidth,self.frame.size.height);
- UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
-
- collectionView.backgroundColor = [UIColor clearColor];
- collectionView.delegate = self;
- collectionView.dataSource = self;
- collectionView.showsVerticalScrollIndicator = NO;
- //注册
- [collectionView registerClass:[YOUPAIHRSearchHeaderCell class] forCellWithReuseIdentifier:@"YOUPAIHRSearchHeaderCell"];
- collectionView;
-
- });
- [self addSubview:_youpaipcollectionView];
- }
- #pragma mark - <UICollectionViewDataSource>
- - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
-
- if (self.youpaiphistoryArray.count>0) {
- NSInteger count = 0;
- if(self.youpaiphistoryArray.count >= 10){
- count = 10;
- }else {
- count = self.youpaiphistoryArray.count;
- }
- NSMutableArray *arr = [NSMutableArray new];
- for (int i = 0; i<count; i++) {
- [arr addObject:self.youpaiphistoryArray[i]];
- }
- self.youpaiphistoryArray= arr;
- }
- return self.youpaiphistoryArray.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- YOUPAIHRSearchHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAIHRSearchHeaderCell" forIndexPath:indexPath];
- cell.youpaipnameStr = [NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]];
- return cell;
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
- UICollectionReusableView *reusableview = nil;
- return reusableview;
- }
- #pragma mark - item宽高
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- NSString *str = [NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]];
- CGRect rect = [str boundingRectWithSize:CGSizeMake(KScreenWidth,25) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :LCFont(12)} context:nil];
- CGFloat width = 0;
- if (str.length>=20) {
- width = 160;
- }else{
- width = rect.size.width;
- }
-
- return CGSizeMake(width+30 ,25);
- }
- #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(10,16,20,10);//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
- }
- -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- // YOUPAIZYLabelModel *model = _youpaiplabelArray[indexPath.row];
- // [self.delegate youpaifclickLabel:model];
- if (self.youpaiptapAction) {
- self.youpaiptapAction([NSString stringWithFormat:@"%@",self.youpaiphistoryArray[indexPath.item]]);
- }
- }
- -(void)youpaifclearnSearchHistory:(UIButton*)sender{
-
- [self.youpaipcollectionView removeFromSuperview];
- self.youpaipnotextL.hidden = NO;
- [_youpaiphistoryArray removeAllObjects];
- [NSKeyedArchiver archiveRootObject:_youpaiphistoryArray toFile:KHistorySearchPath];
- [[NSNotificationCenter defaultCenter]postNotificationName:@"changeHisHeaderView" object:nil];;
-
- }
- @end
|