口袋优选

KBFindChannelViewController.m 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // KBFindChannelViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/1/30.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBFindChannelViewController.h"
  9. #import "KBFindChannelModel.h"
  10. #import "KBChildHeaderView.h"
  11. #import "KBTypeButtonHeader.h"
  12. #import "XLPlainFlowLayout.h"
  13. #import "KBHeaderReusableView.h"
  14. #import "KBGoodCollectionCell.h"
  15. #import "KBChildCategoryModel.h"
  16. #import "KBChildGoodModel.h"
  17. #import "KBItemListViewController.h"
  18. #import "KBGoodListViewController.h"
  19. #import "KBGoodDetailViewController.h"
  20. static NSString *headerID = @"headerID";
  21. static NSString *cellID = @"KBGoodCollectionCell";
  22. @interface KBFindChannelViewController () <UICollectionViewDelegate,UICollectionViewDataSource,YHTypeReusableDelegate,YHChildHeaderViewDelegate>{
  23. NSInteger _page;
  24. NSInteger _type;
  25. BOOL _changeType;
  26. }
  27. @property (nonatomic, strong) UICollectionView *collectionView;
  28. @property (nonatomic, strong) KBChildHeaderView *headerView;
  29. @property (nonatomic, strong) KBHeaderReusableView *reusableView;
  30. @property (nonatomic, strong) NSArray *categoryArr;
  31. @property (nonatomic, strong) NSMutableArray *goodsArr;
  32. @end
  33. @implementation KBFindChannelViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. [self initNavBar];
  37. [self configParam];
  38. [self configCollectionView];
  39. [self loadTopCategoryList];
  40. [self loadCategoryGoodsList];
  41. }
  42. - (void)didReceiveMemoryWarning {
  43. [super didReceiveMemoryWarning];
  44. }
  45. - (void)initNavBar {
  46. [self.navigationBar setBackButtonWithTarget:self selector:@selector(backAction)];
  47. [self.navigationBar setNavTitle:self.model.name];
  48. // UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
  49. // search.frame = CGRectMake(0, 0, FITSIZE(22), FITSIZE(22));
  50. // search.backgroundColor = [UIColor clearColor];
  51. // [search setBackgroundImage:[UIImage imageNamed:@"search_gray"] forState:UIControlStateNormal];
  52. // [self.navigationBar setCustomRightButtons:@[search]];
  53. [self.navigationBar setShowNavigationBarBottomLine:YES];
  54. }
  55. - (void)backAction {
  56. [self.navigationController popViewControllerAnimated:YES];
  57. }
  58. - (void)configParam {
  59. _page = 1;
  60. _type = 1; //默认请求推荐的数据
  61. }
  62. - (void)configCollectionView {
  63. XLPlainFlowLayout *flowLayout = [[XLPlainFlowLayout alloc]init];
  64. flowLayout.naviHeight = 0;
  65. CGFloat width = (SCREEN_WIDTH-5)/2;
  66. CGFloat height = width + 102;
  67. flowLayout.itemSize = CGSizeMake(width, height);
  68. flowLayout.minimumLineSpacing = 0;
  69. flowLayout.minimumInteritemSpacing = 0;
  70. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  71. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, kScreenHeight-NavBarHeight) collectionViewLayout:flowLayout];
  72. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  73. [self.collectionView registerClass:[KBHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
  74. self.collectionView.backgroundColor = [UIColor whiteColor];
  75. self.collectionView.showsVerticalScrollIndicator = NO;
  76. self.collectionView.delegate = self;
  77. self.collectionView.dataSource = self;
  78. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  79. [self loadMoreData];
  80. }];
  81. [self.view addSubview: self.collectionView];
  82. }
  83. - (void)creatTopHeaderView:(NSArray *)array {
  84. self.headerView = [[KBChildHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) delegete:self];
  85. self.collectionView.contentInset = UIEdgeInsetsMake(self.headerView.height, 0, 0, 0);
  86. self.headerView.y = -self.headerView.height;
  87. [self.collectionView addSubview:self.headerView];
  88. [self.collectionView scrollToTop];
  89. }
  90. #pragma mark ====================== Load Data ==========
  91. /**
  92. 加载上部分类数据
  93. */
  94. - (void)loadTopCategoryList {
  95. NSDictionary *para = @{@"pid":self.model.Id};
  96. [KBHttp post:CategorySubList params:para success:^(id json) {
  97. self.categoryArr = [NSArray yy_modelArrayWithClass:[KBChildCategoryModel class] json:json[@"data"]];
  98. [self creatTopHeaderView:self.categoryArr];
  99. } failure:^(NSError *error) {
  100. }];
  101. }
  102. /**
  103. 加载下部商品列表
  104. */
  105. - (void)loadCategoryGoodsList {
  106. NSDictionary *para = @{@"page":@(_page),@"scid":self.model.Id,@"type":@(_type)};
  107. [KBHttp post:CategoryGoods params:para success:^(id json) {
  108. if (_changeType) {
  109. [self.goodsArr removeAllObjects];
  110. }
  111. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  112. [self.goodsArr addObjectsFromArray:list];
  113. [self.collectionView reloadData];
  114. [self.collectionView.mj_footer endRefreshing];
  115. } failure:^(NSError *error) {
  116. [self.collectionView.mj_footer endRefreshing];
  117. }];
  118. }
  119. /**
  120. 上拉加载
  121. */
  122. - (void)loadMoreData {
  123. _page++;
  124. _changeType = NO;
  125. [self loadCategoryGoodsList];
  126. }
  127. #pragma mark ============ YHChildHeaderViewDelegate ==========
  128. /**
  129. 点击头部分类列表
  130. */
  131. - (void)YHChildHeaderViewDidSelectedIndex:(NSInteger)index {
  132. KBChildCategoryModel *model = self.categoryArr[index];
  133. KBItemListViewController *itemList = [[KBItemListViewController alloc] init];
  134. itemList.model = model;
  135. [self.navigationController pushViewController:itemList animated:YES];
  136. }
  137. #pragma mark ============ YHTypeReusableDelegate ==========
  138. /**
  139. 推荐 最新 销量
  140. */
  141. - (void)YHTypeReusableViewDidSelectedIndex:(NSInteger)index {
  142. _type = index + 1;
  143. _changeType = YES;
  144. [self loadCategoryGoodsList];
  145. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
  146. }
  147. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  148. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  149. {
  150. return self.goodsArr.count;
  151. }
  152. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  153. {
  154. return CGSizeMake(0, 40);
  155. }
  156. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  157. {
  158. return CGSizeMake(0, 0);
  159. }
  160. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  163. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  164. cell.model = model;
  165. return cell;
  166. }
  167. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  168. if (kind == UICollectionElementKindSectionHeader) {
  169. if (!self.reusableView) {
  170. self.reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath];
  171. self.reusableView.delegate = self;
  172. }
  173. return self.reusableView;
  174. }
  175. return nil;
  176. }
  177. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  178. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  179. if ([model.type isEqualToString:@"1"]) {
  180. //专场
  181. KBGoodListViewController *list = [[KBGoodListViewController alloc] init];
  182. list.cate_id = model.goods_id;
  183. [self.navigationController pushViewController:list animated:YES];
  184. }else {
  185. //详情
  186. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  187. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  188. detailVC.requestModel = requestModel;
  189. [self.navigationController pushViewController:detailVC animated:YES];
  190. }
  191. }
  192. #pragma mark -------------
  193. - (NSMutableArray *)goodsArr {
  194. if (!_goodsArr) {
  195. _goodsArr = [NSMutableArray array];
  196. }
  197. return _goodsArr;
  198. }
  199. @end