123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- //
- // 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 () <UICollectionViewDelegate,UICollectionViewDataSource,YHTypeReusableDelegate,YHChildHeaderViewDelegate>{
- 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
|