// // KBGoodListCidViewController.m // YouHuiProject // // Created by xiaoxi on 2018/2/2. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBGoodListCidViewController.h" #import "KBGoodListViewController.h" #import "KBGoodDetailViewController.h" #import "KBGoodCollectionCell.h" #import "KBOnePicCell.h" #define BottomViewHeight self.bottomGoodsArr.count*FITSIZE(162)+FITSIZE(40) static NSString *const collectionViewCellID = @"KBGoodCollectionCell"; static NSString *const collectionViewBottom = @"YHGoodListBottomView"; static NSString *const tableViewCellID = @"KBOnePicCell"; @interface YHGoodListBottomView () @property (nonatomic, strong) UITableView *tableView; @end @implementation YHGoodListBottomView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; [self initSubviews]; } return self; } - (void)initSubviews { [self addSubview:self.tableView]; UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, FITSIZE(40))]; topView.backgroundColor = [UIColor clearColor]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(topView.width/2-FITSIZE(152)/2, topView.height/2-FITSIZE(12)/2, FITSIZE(152), FITSIZE(12))]; imageView.backgroundColor = [UIColor clearColor]; imageView.image = [UIImage imageNamed:@"special_recommendation"]; [topView addSubview:imageView]; self.tableView.tableHeaderView = topView; } - (void)setGoodsArr:(NSMutableArray *)goodsArr { _goodsArr = goodsArr; [self.tableView reloadData]; } #pragma mark - tableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.goodsArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KBOnePicCell *cell = [KBOnePicCell cellWithTableView:tableView]; KBChildGoodModel *model = self.goodsArr[indexPath.row]; cell.model = model; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(yh_GoodListBottomViewDidSelectItemAt:)]) { [self.delegate yh_GoodListBottomViewDidSelectItemAt:indexPath]; } } #pragma mark - lazy - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain]; _tableView.backgroundView.backgroundColor = [UIColor clearColor]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.scrollEnabled = NO; _tableView.bounces = NO; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.rowHeight = FITSIZE(162); _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; [_tableView registerClass:[KBOnePicCell class] forCellReuseIdentifier:tableViewCellID]; } return _tableView; } @end @interface KBGoodListCidViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *topGoodsArr; @property (nonatomic, strong) NSMutableArray *bottomGoodsArr; @end @implementation KBGoodListCidViewController - (void)viewDidLoad { [super viewDidLoad]; [self initHUD]; [self initNavBar]; [self requestCategoryGoods]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [SVProgressHUD dismiss]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)initNavBar { [self.navigationBar setBackButtonWithTarget:self selector:@selector(backAction)]; [self.navigationBar setShowNavigationBarBottomLine:YES]; [self.navigationBar setNavTitle:@"专场"]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)initSubviews { [SVProgressHUD dismiss]; [self.view addSubview:self.collectionView]; } #pragma mark - HUD - (void)initHUD { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom]; [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]]; [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]]; } #pragma mark - request - (void)requestCategoryGoods { [SVProgressHUD show]; if (self.topRequest == 1) { [KBHttp post:SimilarGoods params:@{@"goods_id":self.goods_id} success:^(id json) { NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json]; if (arr.count > 0) { [self.topGoodsArr addObjectsFromArray:arr]; [self requestRelevantRecommendations]; } else { [self initSubviews]; } } failure:^(NSError *error) { [SVProgressHUD dismiss]; }]; } else { [KBHttp post:GoodlistByGid params:@{@"gid":self.goods_id} success:^(id json) { NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; if (arr.count > 0) { [self.topGoodsArr addObjectsFromArray:arr]; [self requestRelevantRecommendations]; } else { [self initSubviews]; } } failure:^(NSError *error) { [SVProgressHUD dismiss]; }]; } } - (void)requestRelevantRecommendations { [KBHttp post:RelevantRecommendations params:@{@"page":@1,@"cid":self.cid} success:^(id json) { NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; if (arr.count > 0) { [self.bottomGoodsArr addObjectsFromArray:arr]; } [self initSubviews]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; }]; } #pragma mark - goodListBottomView - (void)yh_GoodListBottomViewDidSelectItemAt:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.bottomGoodsArr[indexPath.row]; if ([model.type isEqualToString:@"0"]) { //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; [self.navigationController pushViewController:detailVC animated:YES]; } else { //专场 KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.goods_id; [self.navigationController pushViewController:listVC animated:YES]; } } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.topGoodsArr.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellID forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; cell.backgroundView.backgroundColor = [UIColor whiteColor]; cell.contentView.backgroundColor = [UIColor whiteColor]; KBChildGoodModel *model = self.topGoodsArr[indexPath.item]; cell.model = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { YHGoodListBottomView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:collectionViewBottom forIndexPath:indexPath]; view.delegate = self; view.goodsArr = self.bottomGoodsArr; return view; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(kScreenWidth, BottomViewHeight); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.topGoodsArr[indexPath.item]; if ([model.type isEqualToString:@"0"]) { KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; [self.navigationController pushViewController:detailVC animated:YES]; } else { KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.goods_id; [self.navigationController pushViewController:listVC animated:YES]; } } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; flowLayout.itemSize = CGSizeMake(FITSIZE(184.9), FITSIZE(287)); flowLayout.minimumLineSpacing = FITSIZE(5); flowLayout.minimumInteritemSpacing = FITSIZE(5); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight, kScreenWidth, kScreenHeight-NavBarHeight) collectionViewLayout:flowLayout]; _collectionView.backgroundView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6]; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:collectionViewCellID]; [_collectionView registerClass:[YHGoodListBottomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:collectionViewBottom]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _collectionView; } - (NSMutableArray *)topGoodsArr { if (!_topGoodsArr) { _topGoodsArr = [NSMutableArray array]; } return _topGoodsArr; } - (NSMutableArray *)bottomGoodsArr { if (!_bottomGoodsArr) { _bottomGoodsArr = [NSMutableArray array]; } return _bottomGoodsArr; } @end