| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | ////  KWHisAndHotWordsView.m//  westkissMob////  Created by iOS on 2022/9/14.//#import "KWHisAndHotWordsView.h"#import "KWCustomLayout.h"#import "KWHisKeyWordCell.h"#import "KWHisCollectHeaderView.h"#import "KWNoHisWordsCell.h"@interface KWHisAndHotWordsView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>@end@implementation KWHisAndHotWordsView- (void)reloadData {    self.hisWords = [self.vm getLocalSearchDatas];    @weakify(self);        [self.vm getHotKeyList:^{            [weak_self.collectV reloadData];        }];    [self.collectV reloadData];    }- (instancetype)initWithFrame:(CGRect)frame {    self = [super initWithFrame:frame];    if (self) {        [self configSubV];    }    return self;}-(void)configSubV {    self.vm = [[KWHisAndHotWordsViewModel alloc] init];        self.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];    self.collectV.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];    [self addSubview:self.collectV];    [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(self);    }];        @weakify(self);    [self.vm setDataReload:^{        [weak_self reloadData];    }];}- (UICollectionView *)collectV {    if (!_collectV) {                KWCustomLayout *layout = [[KWCustomLayout alloc] init];        layout.sectionInset = UIEdgeInsetsMake(5, 20, 20, 20);        layout.minimumLineSpacing = 10;        layout.minimumInteritemSpacing = 10;         layout.scrollDirection = UICollectionViewScrollDirectionVertical;                UICollectionView *v = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 400) collectionViewLayout:layout];        v.delegate = self;        v.dataSource = self;        [v registerClass:[KWHisKeyWordCell class] forCellWithReuseIdentifier:@"cell"];        [v registerClass:[KWNoHisWordsCell class] forCellWithReuseIdentifier:@"KWNoHisWordsCell"];        [v registerClass:[KWHisCollectHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];        _collectV = v;    }    return  _collectV;}#pragma mark - UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {    if (self.callBack) {        NSString *title;        if (indexPath.section == 0) {            title = _hisWords[indexPath.item];            self.callBack(title);                    } else {            KWSearchHotKeyModel *keyM = self.vm.hotKeys[indexPath.item];            if (self.hotCallBack) {                self.hotCallBack(keyM);            }                    }    }}- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return  2;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    if (section == 0) {        if (_hisWords.count == 0) {            return 1;        }        return _hisWords.count;    }    return self.vm.hotKeys.count;}- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section == 0 && _hisWords.count == 0) {        KWNoHisWordsCell *ce = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWNoHisWordsCell" forIndexPath:indexPath];        return ce;    }        KWHisKeyWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];    NSString *title;    if (indexPath.section == 0) {        title = _hisWords[indexPath.item];            } else {        title = self.vm.hotKeys[indexPath.item].title;            }    cell.titleLb.text = title;        return cell;}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.section == 0 && _hisWords.count == 0) {        return CGSizeMake(KScreenWidth-40, 90);    }        NSString *title;    if (indexPath.section == 0) {        title = _hisWords[indexPath.item];            } else {        title = self.vm.hotKeys[indexPath.item].title;            }    CGFloat wid = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 34) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:Rob_Regular size:12]} context:nil].size.width + 12;//    CGFloat wid = [title widthForFont:[UIFont fontWithName:Rob_Regular size:12]] + 12;    wid = wid > KScreenWidth-40 ? KScreenWidth - 40 : wid;    return CGSizeMake(wid, 34);    }- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {    if (section == 1 && self.vm.hotKeys.count == 0) {        return CGSizeZero;    }    return CGSizeMake(KScreenWidth-40, 40);}- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {    if ([kind isEqualToString:UICollectionElementKindSectionHeader] ) {        KWHisCollectHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];        header.titleLb.text = indexPath.section == 1 ? @"HOT SEARCH" : @"SEARCH HISTORY";        header.imgV.hidden = indexPath.section == 1;        header.btn.hidden = indexPath.section == 1;        @weakify(self);        header.callBack = ^{            [weak_self.vm cleanLocalHisDatas];            [weak_self reloadData];        };        return  header;    }    return nil;}@end
 |