猎豆优选

LDNewsListViewController.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // LDNewsListViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/12.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDNewsListViewController.h"
  9. #import "LDGoodCollectionCell.h"
  10. static NSString *cellID = @"LDGoodCollectionCell";
  11. @interface LDNewsListViewController ()<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 LDNewsListViewController
  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. [self.navigationBar setNavTitle:@"猎豆头条"];
  52. self.navigationBar.showNavigationBarBottomLine = YES;
  53. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  54. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  55. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  56. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  57. [self.navigationBar setShowNavigationBarBottomLine:YES];
  58. }
  59. - (void)configCollectionView {
  60. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  61. flowLayout.minimumLineSpacing = 5;
  62. flowLayout.minimumInteritemSpacing = 0;
  63. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  64. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  65. [self.collectionView registerClass:[LDGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  66. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  67. self.collectionView.showsVerticalScrollIndicator = NO;
  68. self.collectionView.delegate = self;
  69. self.collectionView.dataSource = self;
  70. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  71. [self loadMoreData];
  72. }];
  73. [self.view addSubview: self.collectionView];
  74. }
  75. - (void)backAction {
  76. [self.navigationController popViewControllerAnimated:YES];
  77. }
  78. /**
  79. 上拉加载
  80. */
  81. - (void)loadMoreData {
  82. _page++;
  83. _changeType = NO;
  84. [self loadCategoryGoodsList];
  85. }
  86. /**
  87. 加载下部商品列表
  88. */
  89. - (void)loadCategoryGoodsList {
  90. NSDictionary *para = @{@"page":@(_page)};
  91. NSString *url = [NSString stringWithFormat:@"%@/api/v2/message_push/LieDouHeadlinesList",BaseURL];
  92. [LDHttp post:url params:para success:^(id json) {
  93. if (_changeType) {
  94. [self.goodsArr removeAllObjects];
  95. }
  96. NSArray *list = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json[@"data"]];
  97. if (list.count > 0) {
  98. [self.collectionView.mj_footer endRefreshing];
  99. }else {
  100. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  101. }
  102. [self.goodsArr addObjectsFromArray:list];
  103. [self.collectionView reloadData];
  104. [_indicatorView stopAnimating];
  105. } failure:^(NSError *error) {
  106. [self.collectionView.mj_footer endRefreshing];
  107. [_indicatorView stopAnimating];
  108. [MBProgressHUD showMessage:@"加载失败"];
  109. }];
  110. }
  111. #pragma mark =============== YHTypeButtonActionDelegate =============
  112. - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(NSInteger)index {
  113. _type = index + 1;
  114. _changeType = YES;
  115. [self loadCategoryGoodsList];
  116. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
  117. }
  118. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  119. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  120. {
  121. return self.goodsArr.count;
  122. }
  123. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  124. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  125. CGFloat width = (SCREEN_WIDTH-5)/2;
  126. CGFloat height = width + 135;
  127. if (![AccountTool isLogin] && ![model.red_active_status boolValue]) {
  128. height = width +112;
  129. }
  130. return CGSizeMake(width, height);
  131. }
  132. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  133. {
  134. return CGSizeMake(0, 0);
  135. }
  136. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  137. {
  138. return CGSizeMake(0, 0);
  139. }
  140. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. LDGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  143. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  144. cell.model = model;
  145. return cell;
  146. }
  147. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  148. LDChildGoodModel *model = self.goodsArr[indexPath.row];
  149. //详情
  150. LDGoodDetailViewController *detailVC = [[LDGoodDetailViewController alloc] init];
  151. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  152. detailVC.requestModel = requestModel;
  153. // LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:ListAction];
  154. // detailVC.eventModel = evevtModel;
  155. [self.navigationController pushViewController:detailVC animated:YES];
  156. }
  157. #pragma mark -------------
  158. - (NSMutableArray *)goodsArr {
  159. if (!_goodsArr) {
  160. _goodsArr = [NSMutableArray array];
  161. }
  162. return _goodsArr;
  163. }
  164. @end