// // ASProductListViewController.m // Asteria // // Created by iOS on 2023/6/12. // #import "ASProductListViewController.h" #import "HomeFlashDealSubCollectCell.h" #import "ASProductListViewModel.h" #import "ASProductListMenuHeaderView.h" #import "ASProductListActiveHeaderView.h" #import "ASProductListImageCell.h" #import "ASProductListTypeDesCell.h" #import "ASMoreBtCell.h" @interface ASProductListViewController () @property (nonatomic, strong) NSArray *topLinkArr; @property (nonatomic, assign) BOOL desMoreOpen; @property (nonatomic, strong) ASProductListViewModel *vm; @property (nonatomic, strong) UICollectionView *collectV; @property (nonatomic, strong) UIButton *searchNavBt; @end @implementation ASProductListViewController - (void)viewDidLoad { [super viewDidLoad]; self.desMoreOpen = false; [self loadSubVs]; [self loasActiveDataAndV]; self.vm = [ASProductListViewModel new]; [self.collectV reloadData]; } - (void)loadSubVs { [self.view addSubview:self.collectV]; [self.customNavBar addSubview:self.searchNavBt]; [self.searchNavBt mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@44); make.trailing.equalTo(self.customNavBar).offset(-10); make.centerY.equalTo(self.customNavBar); }]; [self.collectV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.leading.trailing.equalTo(self.view); make.bottom.equalTo(self.view); }]; } - (void)loasActiveDataAndV { NSMutableArray *tempArr = [NSMutableArray array]; for (int i=0;i < (arc4random()%6); i++) { ASHomeBannerModel *m = [ASHomeBannerModel demoModelWithType:i%3+1]; [tempArr addObject:m]; } self.topLinkArr = tempArr; [self.collectV reloadData]; } // MARK: - delegate datasource - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { } // MARK: - UICollectionViewDataSource - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath { if (indexPath.section == 2 && indexPath.row == 0) { ASProductListImageCell *c = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListImageCell" forIndexPath:indexPath]; [c setImgStr:@"https://img1.baidu.com/it/u=3005611618,1188581709&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313"]; return c; } if (indexPath.section == 3 && indexPath.item == 0) { ASMoreBtCell *btCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASMoreBtCell" forIndexPath:indexPath]; return btCell; } if (indexPath.section == 3 && indexPath.item == 1) { ASProductListTypeDesCell *desCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASProductListTypeDesCell" forIndexPath:indexPath]; [desCell setData:@"LONG WIGS" des:@"Asteria Hair offers long length wigs with different hair colors and styles, find long lace front wig, long black hair wigs, long blonde wigs you’ve been dreaming of, long straight wig and long curly hair lace front wigs are featured, go shopping and find t" moreStatus:self.desMoreOpen]; desCell.moreStatusChange = ^{ self.desMoreOpen = !self.desMoreOpen; [self.collectV reloadData]; }; return desCell; } HomeFlashDealSubCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell" forIndexPath:indexPath]; if (self.vm.productList.count <= indexPath.row-1) { return cell; } ASProductBaseModel *m = self.vm.productList[indexPath.row-1]; cell.model = m; cell.contView.addCartBt.hidden = true; return cell; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 4; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 0 || section == 1) { return 0; } if (section == 3) { return 2; } return self.vm.productList.count + 1; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { CGSize size = CGSizeZero; if (self.topLinkArr.count > 0) { size = CGSizeMake(KScreenWidth, 40); } return size; } if (section == 1) { return CGSizeMake(KScreenWidth, 60); } return CGSizeZero; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) { ASProductListActiveHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView" forIndexPath:indexPath]; [v setData:self.topLinkArr tapBlock:^(ASHomeBannerModel * _Nullable m) { }]; return v; } if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader]) { ASProductListMenuHeaderView *v = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView" forIndexPath:indexPath]; [v setData:@"SORT BY" sortBlock:^{ } menuBlock:^{ }]; return v; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { CGFloat w = KScreenWidth-20; CGFloat collH = w/355.0*118; return CGSizeMake(w, collH);; } if (indexPath.section == 3 && indexPath.item == 1) { if (!self.desMoreOpen) { return CGSizeMake(KScreenWidth-20, 35+60+24); } //设置行间距 NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle1 setLineSpacing:12]; CGFloat h = [@"Asteria Hair offers long length wigs with different hair colors and styles, find long lace front wig, long black hair wigs, long blonde wigs you’ve been dreaming of, long straight wig and long curly hair lace front wigs are featured, go shopping and find t" boundingRectWithSize:CGSizeMake(KScreenWidth-10-35, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName : paragraphStyle1} context:nil].size.height; return CGSizeMake(KScreenWidth-20, 35+60+(h > 24 ? h : 24)); } if (indexPath.section == 3 && indexPath.item == 0) { return CGSizeMake(KScreenWidth-20, 20+36+50); } 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 (section == 0 || section == 1) { return UIEdgeInsetsZero; } return UIEdgeInsetsMake(10, 10, 10, 10); } // MARK: - actions - (void)search_navAction { // TODO: to searchVC } // MARK: - subvs -(UICollectionView *)collectV { if (!_collectV) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0); layout.scrollDirection = UICollectionViewScrollDirectionVertical; 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.alwaysBounceVertical = true; collV.scrollEnabled = true; collV.delegate = self; collV.dataSource = self; collV.showsHorizontalScrollIndicator = false; collV.showsVerticalScrollIndicator = false; [collV registerClass:[HomeFlashDealSubCollectCell class] forCellWithReuseIdentifier:@"HomeFlashDealSubCollectCell"]; [collV registerClass:[ASProductListImageCell class] forCellWithReuseIdentifier:@"ASProductListImageCell"]; [collV registerClass:[ASProductListTypeDesCell class] forCellWithReuseIdentifier:@"ASProductListTypeDesCell"]; [collV registerClass:[ASMoreBtCell class] forCellWithReuseIdentifier:@"ASMoreBtCell"]; [collV registerClass:[ASProductListActiveHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListActiveHeaderView"]; [collV registerClass:[ASProductListMenuHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ASProductListMenuHeaderView"]; _collectV = collV; } return _collectV; } - (UIButton *)searchNavBt { if (!_searchNavBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setImage:[UIImage imageNamed:@"nav_search"] forState:UIControlStateNormal]; [bt addTarget:self action:@selector(search_navAction) forControlEvents:UIControlEventTouchUpInside]; _searchNavBt = bt; } return _searchNavBt; } @end