// // ASHomeViewController.m // Asteria // // Created by iOS on 2023/6/5. // #import "ASHomeViewController.h" #import "ASHomeBannerCell.h" #import "ASCategaryListCell.h" #import "ASHomeTipCell.h" #import "ASHomeBestSellCell.h" #import "ASHomeNewInCellTableViewCell.h" #import "ASHomeImgCell.h" #import "ASHomeFlashDealCell.h" #import "ASHomeLookingCell.h" #import "ASHomeActiveView.h" #import "ASProductListViewController.h" @interface ASHomeViewController () @property (nonatomic, strong) UITableView *tableV; @property (nonatomic, strong) NSArray *listArr; @property (nonatomic, strong) NSArray *topLinkArr; @property (nonatomic, strong, nullable) ASHomeActiveView *activeV; @end @implementation ASHomeViewController - (void)viewDidLoad { [super viewDidLoad]; __block typeof(self) wSelf = self; [self ucHomeStyle:^{ // TODO: 跳转搜索模块 }]; [self addSubV]; [self demoData]; [self.tableV reloadData]; if (self.topLinkArr.count > 0) { self.activeV = [[ASHomeActiveView alloc] initWithFrame:CGRectZero]; self.activeV.tapBlock = ^(ASHomeBannerModel * _Nullable m) { }; self.tableV.tableHeaderView = self.activeV; [self.activeV setData:self.topLinkArr]; } else { self.activeV.tapBlock = nil; self.activeV = nil; self.tableV.tableHeaderView = nil; } } - (void)demoData { _listArr = [ASHomeMainListModel homeDemoDatas]; 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; } - (void)addSubV { [self.view addSubview:self.tableV]; [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.leading.trailing.equalTo(self.view); make.bottom.equalTo(self.view); }]; self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue(), ^{ [self.tableV.mj_header endRefreshing]; [self.tableV reloadData]; }); }]; } // MARK: - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { // banner return; } } // MARK: - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.listArr.count + 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { default: return 1; } return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { // banner ASHomeBannerCell *bcell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBannerCell" forIndexPath:indexPath]; @weakify(self); bcell.selectCallBack = ^(NSInteger index, NSString * _Nonnull title, NSString * _Nonnull typeId, id _Nonnull model) { if (model == nil || ![model isKindOfClass:[ASHomeBannerModel class]]) { return; } ASHomeBannerModel *m = (ASHomeBannerModel *)model; if (m.type == 1) { // [self goto_WKM_GoodsDetailsC:m.tapUrl]; return; } if (m.type == 2) { // NSString *typeId = m.tapUrl; // [self pushToProductList:@"Products" typeid:typeId]; return; } if ([m.tapUrl.lowercaseString hasPrefix:@"http"]) { // [self toWebVC:m.tapUrl name:@"Alipearl"]; } }; NSMutableArray *arr = [NSMutableArray array]; for (int i=0; i<6; i++) { ASHomeBannerModel *m = [ASHomeBannerModel demoModelWithType:i%3]; [arr addObject:m]; } [bcell showData:arr]; return bcell; } if (self.listArr.count <= indexPath.row) { return [UITableViewCell new]; } ASHomeMainListModel *m = self.listArr[indexPath.section-1]; switch (m.showType) { case 1: { // 分类 ASCategaryListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASCategaryListCell" forIndexPath:indexPath]; cell.arr = m.titleArr; cell.selectCategory = ^(ASHomeCategoryModel * _Nonnull model, NSIndexPath * _Nonnull index) { // TODO: - to productlist [self pushToProductList:model.typeId title:model.title]; }; return cell; } case 2: { // tips ASHomeTipCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeTipCell" forIndexPath:indexPath]; return cell; } case 3: {// bestSell ASHomeBestSellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeBestSellCell" forIndexPath:indexPath]; cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) { [ASNetTools login]; }; cell.model = m; return cell; } case 4: { ASHomeNewInCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeNewInCellTableViewCell" forIndexPath:indexPath]; [cell setData:m]; return cell; } case 5: { ASHomeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeImgCell" forIndexPath:indexPath]; cell.tapImgBlock = ^{ // TODO: - }; [cell setImgStr:m.imgUrl]; return cell; } case 6: { ASHomeFlashDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeFlashDealCell" forIndexPath:indexPath]; cell.productClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) { }; cell.proAddCartClick = ^(NSInteger i, ASProductBaseModel * _Nonnull m) { }; cell.model = m; return cell; } case 7: { ASHomeLookingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASHomeLookingCell" forIndexPath:indexPath]; cell.itemClick = ^(NSInteger i, ASHomeCategoryModel * _Nonnull m) { }; cell.model = m; return cell; } default: break; } return [UITableViewCell new]; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *footer = [[UIView alloc] initWithFrame:CGRectZero]; footer.backgroundColor = [UIColor clearColor]; return footer; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footer = [[UIView alloc] initWithFrame:CGRectZero]; footer.backgroundColor = [UIColor clearColor]; return footer; } -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { } // MARK: - nextViews - (void)pushToProductList:(NSString *)typeId title:(NSString *)title { ASProductListViewController *vc = [ASProductListViewController new]; vc.titleStr = title; [self.navigationController pushViewController:vc animated:true]; } // MARK: - subVs -(UITableView *)tableV { if (!_tableV) { UITableView *tabV = [[UITableView alloc] initWithFrame:self.view.bounds]; tabV.delegate = self; tabV.dataSource = self; [tabV baseSet]; tabV.backgroundColor = UIColor.whiteColor; tabV.separatorStyle = UITableViewCellSeparatorStyleNone; [tabV registerClass:[ASHomeBannerCell class] forCellReuseIdentifier:@"ASHomeBannerCell"]; [tabV registerClass:[ASCategaryListCell class] forCellReuseIdentifier:@"ASCategaryListCell"]; [tabV registerClass:[ASHomeTipCell class] forCellReuseIdentifier:@"ASHomeTipCell"]; [tabV registerClass:[ASHomeBestSellCell class] forCellReuseIdentifier:@"ASHomeBestSellCell"]; [tabV registerClass:[ASHomeNewInCellTableViewCell class] forCellReuseIdentifier:@"ASHomeNewInCellTableViewCell"]; [tabV registerClass:[ASHomeImgCell class] forCellReuseIdentifier:@"ASHomeImgCell"]; [tabV registerClass:[ASHomeFlashDealCell class] forCellReuseIdentifier:@"ASHomeFlashDealCell"]; [tabV registerClass:[ASHomeLookingCell class] forCellReuseIdentifier:@"ASHomeLookingCell"]; _tableV = tabV; } return _tableV; } @end