123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- //
- // 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 () <UITableViewDelegate,UITableViewDataSource>
- @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 () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,YHGoodListBottomViewDelegate>
- @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
|