// // ASHomeLookingCell.m // Asteria // // Created by iOS on 2023/6/10. // #import "ASHomeLookingCell.h" #import "KWScrollOffsetView.h" #import "ASHomeLookingCollCell.h" @interface ASHomeLookingCell () @property (nonatomic, strong) UICollectionView *collectV; @property (nonatomic, strong) UILabel *titleLb; @property (nonatomic, strong) KWScrollOffsetView *offsetV; @end @implementation ASHomeLookingCell - (void)setModel:(ASHomeMainListModel *)model { _model = model; self.titleLb.text = model.title; self.offsetV.hidden = model.titleArr.count <= 1; [self.collectV reloadData]; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self loadSubV]; } return self; } - (void)loadSubV { self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"]; self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"]; [self.contentView addSubview:self.titleLb]; [self.contentView addSubview:self.collectV]; [self.contentView addSubview:self.offsetV]; [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(50); make.centerX.equalTo(self.contentView); make.left.greaterThanOrEqualTo(self.contentView).offset(20); make.height.equalTo(@30); }]; CGFloat collH = ((KScreenWidth-30)/2 + 28)*2 + 10; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(0); make.right.equalTo(self.contentView).offset(-0); make.height.equalTo([NSNumber numberWithFloat:collH]); make.top.equalTo(self.titleLb.mas_bottom).offset(30); }]; [self.offsetV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.contentView); make.height.equalTo(@2); make.width.equalTo(@137); make.top.equalTo(self.collectV.mas_bottom).offset(40); make.bottom.equalTo(self.contentView.mas_bottom).offset(-50); }]; } // MARK: - delegate datasource - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.offsetV.isHidden) { return; } CGFloat offset = scrollView.contentOffset.x; CGFloat itemWidth = (KScreenWidth-20); NSArray *arr = self.model.titleArr; if (arr.count > 0) { CGFloat maxOffset = (arr.count * (itemWidth + 10) - 10 ) - (KScreenWidth-20); self.offsetV.offset = offset/maxOffset; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.itemClick) { ASHomeCategoryModel *m = self.model.titleArr[indexPath.row]; self.itemClick(indexPath.row, m); } } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { ASHomeLookingCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASHomeLookingCollCell" forIndexPath:indexPath]; if (indexPath.row >= self.model.titleArr.count) { return cell; } ASHomeCategoryModel *m = self.model.titleArr[indexPath.row]; [cell setData:m]; return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.model.titleArr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat marg = 10; CGFloat w = (KScreenWidth-3*marg)/2; CGFloat collH = ((KScreenWidth-30)/2 + 28); return CGSizeMake(w, collH); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 10; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 10, 0, 10); } - (UILabel *)titleLb { if (!_titleLb) { UILabel *lb = [[UILabel alloc] init]; lb.textColor = [UIColor blackColor]; lb.font = [UIFont fontWithName:Rob_Bold size:24]; lb.textAlignment = NSTextAlignmentCenter; _titleLb = lb; } return _titleLb; } -(UICollectionView *)collectV { if (!_collectV) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; CGFloat collH = ((KScreenWidth-30)/2 + 28)*2 + 10; CGFloat marg = 10; CGFloat w = (KScreenWidth-3*marg)/2; CGFloat itemH = ((KScreenWidth-30)/2 + 28); layout.itemSize = CGSizeMake(w, itemH); UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout]; collV.backgroundColor = [UIColor whiteColor]; collV.alwaysBounceHorizontal = true; collV.scrollEnabled = true; collV.delegate = self; collV.dataSource = self; collV.showsHorizontalScrollIndicator = false; [collV registerClass:[ASHomeLookingCollCell class] forCellWithReuseIdentifier:@"ASHomeLookingCollCell"]; _collectV = collV; } return _collectV; } - (KWScrollOffsetView *)offsetV { if (!_offsetV) { KWScrollOffsetView *v = [[KWScrollOffsetView alloc] initWithFrame:CGRectMake(0, 0, 137, 2)]; v.clipsToBounds = true; v.offWidth = 54; v.offset = 0; _offsetV = v; } return _offsetV; } @end