猎豆优选

LDGoodListViewController.m 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // LDGoodListViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/26.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDGoodListViewController.h"
  9. #import "LDGoodCollectionCell.h"
  10. static NSString *cellID = @"LDGoodCollectionCell";
  11. @interface LDGoodListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. {
  13. NSInteger _page;
  14. NSInteger _type;
  15. BOOL _changeType;
  16. ActivityIndicatorView *_indicatorView;
  17. }
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) NSMutableArray *goodsArr;
  20. @end
  21. @implementation LDGoodListViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self configParam];
  25. [self configNavigationBar];
  26. [self configCollectionView];
  27. [self initHUD];
  28. [self loadCategoryGoodsList];
  29. }
  30. - (void)viewWillAppear:(BOOL)animated {
  31. [super viewWillAppear:animated];
  32. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  33. self.navigationController.navigationBar.hidden = YES;
  34. }
  35. - (void)viewWillDisappear:(BOOL)animated {
  36. [super viewWillDisappear:animated];
  37. [_indicatorView stopAnimating];
  38. }
  39. #pragma mark - HUD
  40. - (void)initHUD {
  41. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  42. _indicatorView = indicatorView;
  43. [_indicatorView startAnimating];
  44. }
  45. - (void)configParam {
  46. _page = 1;
  47. _type = 1; //默认请求推荐的数据
  48. }
  49. - (void)configNavigationBar {
  50. self.view.backgroundColor = [UIColor whiteColor];
  51. if (self.name.length > 0) {
  52. [self.navigationBar setNavTitle:self.name];
  53. }else {
  54. [self.navigationBar setNavTitle:@"专场"];
  55. }
  56. self.navigationBar.showNavigationBarBottomLine = YES;
  57. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  58. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  59. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  60. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  61. [self.navigationBar setShowNavigationBarBottomLine:YES];
  62. }
  63. - (void)configCollectionView {
  64. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  65. flowLayout.minimumLineSpacing = 5;
  66. flowLayout.minimumInteritemSpacing = 0;
  67. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  68. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  69. [self.collectionView registerClass:[LDGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  70. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  71. self.collectionView.showsVerticalScrollIndicator = NO;
  72. self.collectionView.delegate = self;
  73. self.collectionView.dataSource = self;
  74. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  75. [self loadMoreData];
  76. }];
  77. [self.view addSubview: self.collectionView];
  78. }
  79. - (void)backAction {
  80. [self.navigationController popViewControllerAnimated:YES];
  81. }
  82. /**
  83. 上拉加载
  84. */
  85. - (void)loadMoreData {
  86. _page++;
  87. _changeType = NO;
  88. [self loadCategoryGoodsList];
  89. }
  90. /**
  91. 加载下部商品列表
  92. */
  93. - (void)loadCategoryGoodsList {
  94. if (self.topRequest == 1) {
  95. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/bigCouponSearch",BaseURL];
  96. [LDHttp post:url params:@{@"page":@(_page)} success:^(id json) {
  97. if (_changeType) {
  98. [self.goodsArr removeAllObjects];
  99. }
  100. NSArray *list = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json[@"data"]];
  101. [self.goodsArr addObjectsFromArray:list];
  102. if (list.count > 0) {
  103. [self.collectionView.mj_footer endRefreshing];
  104. }else {
  105. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  106. }
  107. [self.collectionView reloadData];
  108. [_indicatorView stopAnimating];
  109. } failure:^(NSError *error) {
  110. [self.collectionView.mj_footer endRefreshing];
  111. [_indicatorView stopAnimating];
  112. }];
  113. }
  114. else {
  115. NSDictionary *para = @{@"sort":@"1",
  116. @"page":@(_page),
  117. @"category_id":self.cate_id,
  118. @"stype":@"0"};
  119. [LDHttp post:StocklistByCategoryId params:para success:^(id json) {
  120. if (_changeType) {
  121. [self.goodsArr removeAllObjects];
  122. }
  123. NSArray *list = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json[@"data"]];
  124. if (list.count > 0) {
  125. [self.collectionView.mj_footer endRefreshing];
  126. }else {
  127. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  128. }
  129. [self.goodsArr addObjectsFromArray:list];
  130. [self.collectionView reloadData];
  131. [_indicatorView stopAnimating];
  132. } failure:^(NSError *error) {
  133. [self.collectionView.mj_footer endRefreshing];
  134. [_indicatorView stopAnimating];
  135. [MBProgressHUD showMessage:@"加载失败"];
  136. }];
  137. }
  138. }
  139. #pragma mark =============== YHTypeButtonActionDelegate =============
  140. - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(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 sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  152. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  153. CGFloat width = (SCREEN_WIDTH-5)/2;
  154. CGFloat height = width + 135;
  155. if (![AccountTool isLogin] && ![model.red_active_status boolValue]) {
  156. height = width +112;
  157. }
  158. return CGSizeMake(width, height);
  159. }
  160. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  161. {
  162. return CGSizeMake(0, 0);
  163. }
  164. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  165. {
  166. return CGSizeMake(0, 0);
  167. }
  168. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. LDGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  171. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  172. cell.model = model;
  173. return cell;
  174. }
  175. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  176. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  177. if ([model.type isEqualToString:@"1"]) {
  178. //专场
  179. LDGoodListViewController *list = [[LDGoodListViewController alloc] init];
  180. list.cate_id = model.goods_id;
  181. list.topRequest = 1;
  182. [self.navigationController pushViewController:list animated:YES];
  183. }else {
  184. //详情
  185. LDGoodDetailViewController *detailVC = [[LDGoodDetailViewController alloc] init];
  186. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  187. detailVC.requestModel = requestModel;
  188. LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:ListAction];
  189. detailVC.eventModel = evevtModel;
  190. [self.navigationController pushViewController:detailVC animated:YES];
  191. }
  192. }
  193. #pragma mark -------------
  194. - (NSMutableArray *)goodsArr {
  195. if (!_goodsArr) {
  196. _goodsArr = [NSMutableArray array];
  197. }
  198. return _goodsArr;
  199. }
  200. - (void)didReceiveMemoryWarning {
  201. [super didReceiveMemoryWarning];
  202. // Dispose of any resources that can be recreated.
  203. }
  204. /*
  205. #pragma mark - Navigation
  206. // In a storyboard-based application, you will often want to do a little preparation before navigation
  207. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  208. // Get the new view controller using [segue destinationViewController].
  209. // Pass the selected object to the new view controller.
  210. }
  211. */
  212. @end