// // PIHoemCategoryCollectionView.m // YuMi // // Created by duoban on 2024/2/21. // #import "PIHoemCategoryCollectionView.h" #import "PIHomeCategoryTitleModel.h" #import "PIHoemCategoryTitleCell.h" @interface PIHoemCategoryCollectionView() @property(nonatomic,strong) UICollectionView *pi_collectionView; @end @implementation PIHoemCategoryCollectionView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if(self){ [self installUI]; [self installConstraints]; } return self; } -(void)installUI{ [self addSubview:self.pi_collectionView]; } -(void)installConstraints{ [self.pi_collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; } -(void)setTitleList:(NSMutableArray *)titleList{ _titleList = titleList; [self.pi_collectionView reloadData]; } #pragma mark- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.titleList.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ PIHomeCategoryTitleModel *model = [self.titleList xpSafeObjectAtIndex:indexPath.row]; CGFloat width = model.isChecked ? model.checkedWidth : model.noCheckedWidth; return CGSizeMake(width + kGetScaleWidth(10), kGetScaleWidth(44)); } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ PIHoemCategoryTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PIHoemCategoryTitleCell class]) forIndexPath:indexPath]; PIHomeCategoryTitleModel *model = [self.titleList xpSafeObjectAtIndex:indexPath.row]; cell.tagModel = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ for (PIHomeCategoryTitleModel *model in self.titleList) { model.isChecked = NO; } PIHomeCategoryTitleModel *selectModel = [self.titleList xpSafeObjectAtIndex:indexPath.row]; selectModel.isChecked = YES; [self.pi_collectionView reloadData]; if(self.delegate && [self.delegate respondsToSelector:@selector(pi_didSelectItemAtIndex:)]){ [self.delegate pi_didSelectItemAtIndex:indexPath.row]; } // int i = [selectModel.id isEqualToString:@"-1"] ? 0 : 1; // if(self.scrolledHandle){ // self.scrolledHandle(i); // } } - (void)setIndex:(NSInteger)index{ _index = index; for (int i = 0; i < self.titleList.count; i++) { PIHomeCategoryTitleModel *model = [self.titleList xpSafeObjectAtIndex:i]; model.isChecked = NO; } PIHomeCategoryTitleModel *curModel = [self.titleList xpSafeObjectAtIndex:_index]; curModel.isChecked = YES; [self.pi_collectionView reloadData]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.pi_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; }); } #pragma mark - 懒加载 - (UICollectionView *)pi_collectionView{ if (!_pi_collectionView) { MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init]; layout.minimumLineSpacing = kGetScaleWidth(10); layout.minimumInteritemSpacing = kGetScaleWidth(10); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.sectionInset = UIEdgeInsetsMake(0, kGetScaleWidth(10), 0, kGetScaleWidth(10)); _pi_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _pi_collectionView.dataSource = self; _pi_collectionView.delegate = self; _pi_collectionView.tag = 98777; _pi_collectionView.showsVerticalScrollIndicator = NO; _pi_collectionView.showsHorizontalScrollIndicator = NO; _pi_collectionView.backgroundColor = [UIColor clearColor]; [_pi_collectionView registerClass:[PIHoemCategoryTitleCell class] forCellWithReuseIdentifier:NSStringFromClass([PIHoemCategoryTitleCell class])]; } return _pi_collectionView; } @end