// // TFFiltrateCollectionView.m // TFReader // // Created by 谢腾飞 on 2020/12/25. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFFiltrateCollectionView.h" #import "TFFiltrateItemViewCell.h" @interface TFFiltrateCollectionView () @property (nonatomic ,strong) UICollectionViewFlowLayout *flowLayout; @property (nonatomic ,strong) UICollectionView *collectionView; @end @implementation TFFiltrateCollectionView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; [self createSubViews]; } return self; } - (void)createSubViews { [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; UIView *line = [[UIView alloc] init]; line.backgroundColor = kGrayLineColor; [self addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kHalfMargin); make.bottom.equalTo(self.collectionView); make.width.mas_equalTo(self.mas_width); make.height.mas_equalTo(kCellLineHeight); }]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.searchModel.searchList.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { TFOptionListModel *t_list = [self.searchModel.searchList objectOrNilAtIndex:indexPath.row]; TFFiltrateItemViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TFFiltrateItemViewCell" forIndexPath:indexPath]; cell.optionModel = t_list; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { TFOptionListModel *t_list = [self.searchModel.searchList objectOrNilAtIndex:indexPath.row]; if ([self.delegate respondsToSelector:@selector(selectFiltrateViewWithIndexPath:selectKeyword:selectValue:)]) { [self.delegate selectFiltrateViewWithIndexPath:indexPath selectKeyword:self.searchModel.field selectValue:t_list.value]; } } - (void)setSearchModel:(TFSearchBoxModel *)searchModel { #if !TF_Free_Mode NSMutableArray *t_array = [searchModel.searchList mutableCopy]; for (TFOptionListModel *t_model in t_array) { if ([t_model.display isEqualToString:TFLocalizedString(@"免费")]) { [t_array removeObject:t_model]; break; } } searchModel.searchList = [t_array copy]; #elif !TF_Super_Member_Mode NSMutableArray *t_array = [searchModel.searchList mutableCopy]; for (TFOptionListModel *t_model in t_array) { if ([t_model.display isEqualToString:TFLocalizedString(@"会员")]) { [t_array removeObject:t_model]; break; } } searchModel.searchList = [t_array copy]; #endif _searchModel = searchModel; [self.collectionView reloadData]; } - (UICollectionView *)collectionView { if (!_collectionView) { _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout]; _collectionView.userInteractionEnabled = YES; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.alwaysBounceHorizontal = YES; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[TFFiltrateItemViewCell class] forCellWithReuseIdentifier:@"TFFiltrateItemViewCell"]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _collectionView; } - (UICollectionViewFlowLayout *)flowLayout { if (!_flowLayout) { _flowLayout = [[UICollectionViewFlowLayout alloc] init]; _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _flowLayout.estimatedItemSize = CGSizeMake(30, 30); _flowLayout.minimumLineSpacing = kQuarterMargin; _flowLayout.sectionInset = UIEdgeInsetsMake(0, kQuarterMargin, 0, kQuarterMargin); } return _flowLayout; } @end