// // DRFindChannelViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/30. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRFindChannelViewController.h" #import "DRFindChannelModel.h" #import "DRChildHeaderView.h" #import "DRTypeButtonHeader.h" #import "XLPlainFlowLayout.h" #import "DRHeaderReusableView.h" #import "DRGoodCollectionCell.h" #import "DRChildCategoryModel.h" #import "DRChildGoodModel.h" #import "DRItemListViewController.h" #import "DRGoodListViewController.h" #import "DRGoodDetailViewController.h" static NSString *headerID = @"headerID"; static NSString *cellID = @"DRGoodCollectionCell"; @interface DRFindChannelViewController () { NSInteger _page; NSInteger _type; BOOL _changeType; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) DRChildHeaderView *headerView; @property (nonatomic, strong) DRHeaderReusableView *reusableView; @property (nonatomic, strong) NSArray *categoryArr; @property (nonatomic, strong) NSMutableArray *goodsArr; @end @implementation DRFindChannelViewController - (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:[DRGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [self.collectionView registerClass:[DRHeaderReusableView 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 = [[DRChildHeaderView 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}; [DRHttp post:CategorySubList params:para success:^(id json) { self.categoryArr = [NSArray yy_modelArrayWithClass:[DRChildCategoryModel class] json:json[@"data"]]; [self creatTopHeaderView:self.categoryArr]; } failure:^(NSError *error) { }]; } /** 加载下部商品列表 */ - (void)loadCategoryGoodsList { NSDictionary *para = @{@"page":@(_page),@"scid":self.model.Id,@"type":@(_type)}; [DRHttp post:CategoryGoods params:para success:^(id json) { if (_changeType) { [self.goodsArr removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[DRChildGoodModel 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 { DRChildCategoryModel *model = self.categoryArr[index]; DRItemListViewController *itemList = [[DRItemListViewController 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 { DRGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; DRChildGoodModel *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 { DRChildGoodModel *model = self.goodsArr[indexPath.row]; if ([model.type isEqualToString:@"1"]) { //专场 DRGoodListViewController *list = [[DRGoodListViewController alloc] init]; list.cate_id = model.goods_id; list.topRequest = 1; [self.navigationController pushViewController:list animated:YES]; }else { //详情 DRGoodDetailViewController *detailVC = [[DRGoodDetailViewController 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