// // KWMineMoreProductsCell.m // westkissMob // // Created by iOS on 2022/9/15. // #import "KWMineMoreProductsCell.h" #import "KWMineMoreProductTypeCell.h" #import "HomeFlashDealSubCollectCell.h" @interface KWMineMoreProductsCell () @property (nonatomic, strong) HomeFilterModel *currentKey; @property (nonatomic, strong) NSArray *keyWidthArr; @end @implementation KWMineMoreProductsCell - (void)setTopSpace:(CGFloat)topSpace { _topSpace = topSpace; [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(topSpace); make.centerX.equalTo(self.contentView); make.left.greaterThanOrEqualTo(self.contentView).offset(20); make.height.equalTo(@34); make.width.greaterThanOrEqualTo(@80); }]; } -(void)setData:(KWMineMoreProductModel *)model { self.model = model; for (HomeFilterModel *type in self.model.keysArr) { if (type.selected) { self.currentKey = type; } } if (!self.currentKey) { self.currentKey = self.model.keysArr.firstObject; } self.titleLb.text = self.model.title; [self calulateItemWidth]; [self.typeCollectV reloadData]; [self.collectV reloadData]; } -(void)calulateItemWidth { NSMutableArray *tempArr = [NSMutableArray array]; NSDictionary *attribute = @{NSFontAttributeName: self.titleLb.font}; for (HomeFilterModel *typeM in self.model.keysArr) { NSString *key = typeM.title; CGFloat wid = [key boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 34) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size.width + 10; // CGFloat wid = [key widthForFont:[UIFont fontWithName:Rob_Regular size:12]] + 10; [tempArr addObject:[NSNumber numberWithFloat:wid]]; } int numLins = tempArr.count > 0 ? 1 : 0; CGFloat totalWid = 0; CGFloat maxWid = KScreenWidth - 20; int lineBeginI = 0; for (int i = 0; i < tempArr.count; i++) { CGFloat tempW = tempArr[i].floatValue; if ((tempW + totalWid + (i-lineBeginI)*10) > maxWid) { if (i>lineBeginI) { int num = i-lineBeginI; CGFloat elseSpace = (maxWid - totalWid - (num-1)*10)/num; for (int j = lineBeginI; jlineBeginI) { int num = (int)(tempArr.count) - lineBeginI; CGFloat elseSpace = (maxWid - totalWid- (num-1)*10)/num; for (int j = lineBeginI; j *arr = self.currentKey.list; 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 (collectionView == self.typeCollectV) { for (int i = 0; i < self.model.keysArr.count; i++) { HomeFilterModel *item = self.model.keysArr[i]; item.selected = i == indexPath.row; } HomeFilterModel *key = self.model.keysArr[indexPath.row]; self.currentKey = key; [self.typeCollectV reloadData]; [self.collectV reloadData]; return; } if (self.productClick) { NSArray *arr = self.currentKey.list; ASProductBaseModel *m = arr[indexPath.row]; self.productClick(indexPath.row, m); } } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { if (collectionView == self.typeCollectV) { KWMineMoreProductTypeCell *ce = [collectionView dequeueReusableCellWithReuseIdentifier:@"KWMineMoreProductTypeCell" forIndexPath:indexPath]; HomeFilterModel *ti = self.model.keysArr[indexPath.row]; [ce setTitle:ti.title isSelect:ti.selected]; return ce; } HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath]; NSArray *arr = self.currentKey.list; ASProductBaseModel *m = arr[indexPath.row]; cell.model = m; return cell; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (collectionView == self.typeCollectV) { return self.model.keysArr.count; } NSArray *arr = self.currentKey.list; return arr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView == self.typeCollectV) { CGFloat wid = self.keyWidthArr[indexPath.row].floatValue - 0.5; return CGSizeMake(wid, 40); } CGFloat marg = 10; CGFloat w = (KScreenWidth-3*marg)/2; CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg; 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 { if (collectionView == self.typeCollectV) { return UIEdgeInsetsMake(10, 10, 10, 10); } return UIEdgeInsetsMake(20, 0, 10, 0); } @end