猎豆优选

LDFindChannelViewController.m 8.1KB

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