// // 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 () @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 - - (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=20) { width = 160; }else{ width = rect.size.width; } return CGSizeMake(width+30 ,25); } #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(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