// // KBFindChannelViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/30. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBFindChannelViewController.h" #import "KBFindChannelModel.h" #import "KBChildHeaderView.h" #import "KBTypeButtonHeader.h" #import "XLPlainFlowLayout.h" #import "KBHeaderReusableView.h" #import "KBGoodCollectionCell.h" #import "KBChildCategoryModel.h" #import "KBChildGoodModel.h" #import "KBItemListViewController.h" #import "KBGoodListViewController.h" #import "KBGoodDetailViewController.h" static NSString *headerID = @"headerID"; static NSString *cellID = @"KBGoodCollectionCell"; @interface KBFindChannelViewController () { NSInteger _page; NSInteger _type; BOOL _changeType; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) KBChildHeaderView *headerView; @property (nonatomic, strong) KBHeaderReusableView *reusableView; @property (nonatomic, strong) NSArray *categoryArr; @property (nonatomic, strong) NSMutableArray *goodsArr; @end @implementation KBFindChannelViewController - (void)viewDidLoad { [super viewDidLoad]; [self initNavBar]; [self configParam]; [self configCollectionView]; [self loadTopCategoryList]; [self loadCategoryGoodsList]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)initNavBar { [self.navigationBar setBackButtonWithTarget:self selector:@selector(backAction)]; [self.navigationBar setNavTitle:self.model.name]; // UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom]; // search.frame = CGRectMake(0, 0, FITSIZE(22), FITSIZE(22)); // search.backgroundColor = [UIColor clearColor]; // [search setBackgroundImage:[UIImage imageNamed:@"search_gray"] forState:UIControlStateNormal]; // [self.navigationBar setCustomRightButtons:@[search]]; [self.navigationBar setShowNavigationBarBottomLine:YES]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)configParam { _page = 1; _type = 1; //默认请求推荐的数据 } - (void)configCollectionView { XLPlainFlowLayout *flowLayout = [[XLPlainFlowLayout alloc]init]; flowLayout.naviHeight = 0; CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 102; flowLayout.itemSize = CGSizeMake(width, height); flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, kScreenHeight-NavBarHeight) collectionViewLayout:flowLayout]; [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [self.collectionView registerClass:[KBHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ [self loadMoreData]; }]; [self.view addSubview: self.collectionView]; } - (void)creatTopHeaderView:(NSArray *)array { self.headerView = [[KBChildHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) delegete:self]; self.collectionView.contentInset = UIEdgeInsetsMake(self.headerView.height, 0, 0, 0); self.headerView.y = -self.headerView.height; [self.collectionView addSubview:self.headerView]; [self.collectionView scrollToTop]; } #pragma mark ====================== Load Data ========== /** 加载上部分类数据 */ - (void)loadTopCategoryList { NSDictionary *para = @{@"pid":self.model.Id}; [KBHttp post:CategorySubList params:para success:^(id json) { self.categoryArr = [NSArray yy_modelArrayWithClass:[KBChildCategoryModel class] json:json[@"data"]]; [self creatTopHeaderView:self.categoryArr]; } failure:^(NSError *error) { }]; } /** 加载下部商品列表 */ - (void)loadCategoryGoodsList { NSDictionary *para = @{@"page":@(_page),@"scid":self.model.Id,@"type":@(_type)}; [KBHttp post:CategoryGoods params:para success:^(id json) { if (_changeType) { [self.goodsArr removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; [self.goodsArr addObjectsFromArray:list]; [self.collectionView reloadData]; [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; }]; } /** 上拉加载 */ - (void)loadMoreData { _page++; _changeType = NO; [self loadCategoryGoodsList]; } #pragma mark ============ YHChildHeaderViewDelegate ========== /** 点击头部分类列表 */ - (void)YHChildHeaderViewDidSelectedIndex:(NSInteger)index { KBChildCategoryModel *model = self.categoryArr[index]; KBItemListViewController *itemList = [[KBItemListViewController alloc] init]; itemList.model = model; [self.navigationController pushViewController:itemList animated:YES]; } #pragma mark ============ YHTypeReusableDelegate ========== /** 推荐 最新 销量 */ - (void)YHTypeReusableViewDidSelectedIndex:(NSInteger)index { _type = index + 1; _changeType = YES; [self loadCategoryGoodsList]; [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES]; } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.goodsArr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(0, 40); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; KBChildGoodModel *model = self.goodsArr[indexPath.row]; cell.model = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (kind == UICollectionElementKindSectionHeader) { if (!self.reusableView) { self.reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath]; self.reusableView.delegate = self; } return self.reusableView; } return nil; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.row]; if ([model.type isEqualToString:@"1"]) { //专场 KBGoodListViewController *list = [[KBGoodListViewController alloc] init]; list.cate_id = model.goods_id; [self.navigationController pushViewController:list animated:YES]; }else { //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; [self.navigationController pushViewController:detailVC animated:YES]; } } #pragma mark ------------- - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } @end