123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- //
- // KWMineMoreProductsCell.m
- // westkissMob
- //
- // Created by iOS on 2022/9/15.
- //
- #import "KWMineMoreProductsCell.h"
- #import "KWMineMoreProductTypeCell.h"
- #import "HomeFlashDealSubCollectCell.h"
- @interface KWMineMoreProductsCell ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
- @property (nonatomic, strong) HomeFilterModel *currentKey;
- @property (nonatomic, strong) NSArray<NSNumber *> *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<NSNumber *> *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; j<i; j++) {
- CGFloat tempSubW = tempArr[j].floatValue;
- tempArr[j] = [NSNumber numberWithFloat: tempSubW + elseSpace];
- }
- }
- if (i == tempArr.count - 1) {
- tempArr[i] = [NSNumber numberWithFloat: maxWid];
- break;
- }
- totalWid = tempW;
- lineBeginI = i;
- numLins += 1;
- } else {
- totalWid += tempW;
- if (i == tempArr.count - 1) {
- if (tempArr.count>lineBeginI) {
- int num = (int)(tempArr.count) - lineBeginI;
- CGFloat elseSpace = (maxWid - totalWid- (num-1)*10)/num;
- for (int j = lineBeginI; j<tempArr.count; j++) {
- CGFloat tempSubW = tempArr[j].floatValue;
- tempArr[j] = [NSNumber numberWithFloat: tempSubW + elseSpace];
- }
- }
- }
- }
- }
- self.keyWidthArr = tempArr;
- @weakify(self);
- [self.typeCollectV mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(weak_self.titleLb.mas_bottom).offset(20);
- make.height.equalTo([NSNumber numberWithInteger:20 + numLins * 40 + (numLins-1)*10]);
- make.left.right.equalTo(weak_self.contentView);
- }];
- }
- - (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.typeCollectV];
- [self.contentView addSubview:self.collectV];
- [self.contentView addSubview:self.offsetV];
-
-
- [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.contentView).offset(20);
- make.centerX.equalTo(self.contentView);
- make.left.greaterThanOrEqualTo(self.contentView).offset(20);
- make.height.equalTo(@34);
- make.width.greaterThanOrEqualTo(@80);
- }];
-
- [self.typeCollectV mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLb.mas_bottom).offset(20);
- make.height.equalTo(@110);
- make.left.right.equalTo(self.contentView);
- }];
-
- CGFloat collH = (KScreenWidth-30)/2 + productHWithOutImg;
- [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(10);
- make.right.equalTo(self.contentView).offset(-10);
- make.height.equalTo([NSNumber numberWithFloat:collH]);
- make.top.equalTo(self.typeCollectV.mas_bottom).offset(10);
- }];
- [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(20);
- make.bottom.equalTo(self.contentView).offset(-40);
- }];
-
-
- }
- - (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;
- }
- - (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;
- }
- -(UICollectionView *)collectV {
- if (!_collectV) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- CGFloat collH = (KScreenWidth-30)/2 + 126;
- layout.itemSize = CGSizeMake((KScreenWidth - 30)/2, collH);
- UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, collH) collectionViewLayout:layout];
- collV.backgroundColor = [UIColor whiteColor];
- collV.alwaysBounceHorizontal = true;
- collV.delegate = self;
- collV.dataSource = self;
- collV.showsHorizontalScrollIndicator = false;
- [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"];
- _collectV = collV;
- }
- return _collectV;
- }
- -(UICollectionView *)typeCollectV {
- if (!_typeCollectV) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
- layout.scrollDirection = UICollectionViewScrollDirectionVertical;
- layout.itemSize = CGSizeMake(100, 40);
- UICollectionView *collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 110) collectionViewLayout:layout];
- collV.scrollEnabled = false;
- collV.backgroundColor = [UIColor whiteColor];
- collV.alwaysBounceVertical = true;
- collV.delegate = self;
- collV.dataSource = self;
- collV.showsVerticalScrollIndicator = false;
- collV.showsHorizontalScrollIndicator = false;
- [collV registerClass:[KWMineMoreProductTypeCell class] forCellWithReuseIdentifier:@"KWMineMoreProductTypeCell"];
- _typeCollectV = collV;
- }
- return _typeCollectV;
- }
- - (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
- }
- // MARK: - UICollectionViewDelegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- if (self.offsetV.isHidden) {
- return;
- }
- CGFloat offset = scrollView.contentOffset.x;
- CGFloat itemWidth = (KScreenWidth-30)/2;
- NSArray<ASProductBaseModel*> *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<ASProductBaseModel*> *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<ASProductBaseModel*> *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<ASProductBaseModel*> *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
|