《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRGoodListViewController.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // DRGoodListViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/26.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRGoodListViewController.h"
  9. #import "DRGoodCollectionCell.h"
  10. #import "DRGoodDetailViewController.h"
  11. static NSString *cellID = @"DRGoodCollectionCell";
  12. @interface DRGoodListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  13. {
  14. NSInteger _page;
  15. NSInteger _type;
  16. BOOL _changeType;
  17. ActivityIndicatorView *_indicatorView;
  18. }
  19. @property (nonatomic, strong) UICollectionView *collectionView;
  20. @property (nonatomic, strong) NSMutableArray *goodsArr;
  21. @end
  22. @implementation DRGoodListViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self configParam];
  26. [self configNavigationBar];
  27. [self configCollectionView];
  28. [self initHUD];
  29. [self loadCategoryGoodsList];
  30. }
  31. - (void)viewWillAppear:(BOOL)animated {
  32. [super viewWillAppear:animated];
  33. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  34. self.navigationController.navigationBar.hidden = YES;
  35. }
  36. - (void)viewWillDisappear:(BOOL)animated {
  37. [super viewWillDisappear:animated];
  38. [_indicatorView stopAnimating];
  39. }
  40. #pragma mark - HUD
  41. - (void)initHUD {
  42. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  43. _indicatorView = indicatorView;
  44. [_indicatorView startAnimating];
  45. }
  46. - (void)configParam {
  47. _page = 1;
  48. _type = 1; //默认请求推荐的数据
  49. }
  50. - (void)configNavigationBar {
  51. self.view.backgroundColor = [UIColor whiteColor];
  52. if (self.name.length > 0) {
  53. [self.navigationBar setNavTitle:self.name];
  54. }else {
  55. [self.navigationBar setNavTitle:@"专场"];
  56. }
  57. self.navigationBar.showNavigationBarBottomLine = YES;
  58. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  59. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  60. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  61. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  62. [self.navigationBar setShowNavigationBarBottomLine:YES];
  63. }
  64. - (void)configCollectionView {
  65. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  66. flowLayout.minimumLineSpacing = 5;
  67. flowLayout.minimumInteritemSpacing = 0;
  68. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  69. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  70. [self.collectionView registerClass:[DRGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  71. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  72. self.collectionView.showsVerticalScrollIndicator = NO;
  73. self.collectionView.delegate = self;
  74. self.collectionView.dataSource = self;
  75. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  76. [self loadMoreData];
  77. }];
  78. [self.view addSubview: self.collectionView];
  79. }
  80. - (void)backAction {
  81. [self.navigationController popViewControllerAnimated:YES];
  82. }
  83. /**
  84. 上拉加载
  85. */
  86. - (void)loadMoreData {
  87. _page++;
  88. _changeType = NO;
  89. [self loadCategoryGoodsList];
  90. }
  91. /**
  92. 加载下部商品列表
  93. */
  94. - (void)loadCategoryGoodsList {
  95. if (self.topRequest == 1) {
  96. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/bigCouponSearch",BaseURL];
  97. [DRHttp post:url params:@{@"page":@(_page)} success:^(id json) {
  98. if (_changeType) {
  99. [self.goodsArr removeAllObjects];
  100. }
  101. NSArray *list = [NSArray yy_modelArrayWithClass:[DRChildGoodModel class] json:json[@"data"]];
  102. [self.goodsArr addObjectsFromArray:list];
  103. if (list.count > 0) {
  104. [self.collectionView.mj_footer endRefreshing];
  105. }else {
  106. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  107. }
  108. [self.collectionView reloadData];
  109. [_indicatorView stopAnimating];
  110. } failure:^(NSError *error) {
  111. [self.collectionView.mj_footer endRefreshing];
  112. [_indicatorView stopAnimating];
  113. }];
  114. }
  115. else {
  116. NSDictionary *para = @{@"sort":@"1",
  117. @"page":@(_page),
  118. @"category_id":self.cate_id,
  119. @"stype":@"0"};
  120. [DRHttp post:StocklistByCategoryId params:para success:^(id json) {
  121. if (_changeType) {
  122. [self.goodsArr removeAllObjects];
  123. }
  124. NSArray *list = [NSArray yy_modelArrayWithClass:[DRChildGoodModel class] json:json[@"data"]];
  125. if (list.count > 0) {
  126. [self.collectionView.mj_footer endRefreshing];
  127. }else {
  128. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  129. }
  130. [self.goodsArr addObjectsFromArray:list];
  131. [self.collectionView reloadData];
  132. [_indicatorView stopAnimating];
  133. } failure:^(NSError *error) {
  134. [self.collectionView.mj_footer endRefreshing];
  135. [_indicatorView stopAnimating];
  136. [MBProgressHUD showMessage:@"加载失败"];
  137. }];
  138. }
  139. }
  140. #pragma mark =============== YHTypeButtonActionDelegate =============
  141. - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(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 sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  153. DRChildGoodModel *model = self.goodsArr[indexPath.row];
  154. CGFloat width = (SCREEN_WIDTH-5)/2;
  155. CGFloat height = width + 112;
  156. if (model.commission_price.length > 0) {
  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. DRGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  171. DRChildGoodModel *model = self.goodsArr[indexPath.row];
  172. cell.model = model;
  173. return cell;
  174. }
  175. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  176. DRChildGoodModel *model = self.goodsArr[indexPath.row];
  177. if ([model.type isEqualToString:@"1"]) {
  178. //专场
  179. DRGoodListViewController *list = [[DRGoodListViewController alloc] init];
  180. list.cate_id = model.goods_id;
  181. list.topRequest = 1;
  182. [self.navigationController pushViewController:list animated:YES];
  183. }else {
  184. //详情
  185. DRGoodDetailViewController *detailVC = [[DRGoodDetailViewController alloc] init];
  186. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  187. detailVC.requestModel = requestModel;
  188. DREventModel *evevtModel = [[DREventModel 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