天天省钱快报

KBGoodListViewController.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // KBGoodListViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/26.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBGoodListViewController.h"
  9. #import "KBGoodCollectionCell.h"
  10. #import "KBGoodDetailViewController.h"
  11. static NSString *cellID = @"KBGoodCollectionCell";
  12. @interface KBGoodListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  13. {
  14. NSInteger _page;
  15. NSInteger _type;
  16. BOOL _changeType;
  17. }
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @property (nonatomic, strong) NSMutableArray *goodsArr;
  20. @end
  21. @implementation KBGoodListViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. [self initHUD];
  25. [self configParam];
  26. [self configNavigationBar];
  27. [self configCollectionView];
  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. [SVProgressHUD dismiss];
  38. }
  39. #pragma mark - HUD
  40. - (void)initHUD {
  41. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
  42. [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]];
  43. [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]];
  44. [SVProgressHUD show];
  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:[KBGoodCollectionCell 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 = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  76. [self loadMoreData];
  77. }];
  78. self.collectionView.footRefreshState = MJFooterRefreshStateNormal;
  79. [self.view addSubview: self.collectionView];
  80. }
  81. - (void)backAction {
  82. [self.navigationController popViewControllerAnimated:YES];
  83. }
  84. /**
  85. 上拉加载
  86. */
  87. - (void)loadMoreData {
  88. _page++;
  89. _changeType = NO;
  90. [self loadCategoryGoodsList];
  91. }
  92. /**
  93. 加载下部商品列表
  94. */
  95. - (void)loadCategoryGoodsList {
  96. if (self.topRequest == 1) {
  97. [KBHttp post:SimilarGoods params:@{@"goods_id":self.cate_id} success:^(id json) {
  98. if (_changeType) {
  99. [self.goodsArr removeAllObjects];
  100. }
  101. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json];
  102. [self.goodsArr addObjectsFromArray:list];
  103. [self.collectionView reloadData];
  104. [SVProgressHUD dismiss];
  105. [self.collectionView.mj_footer endRefreshing];
  106. } failure:^(NSError *error) {
  107. [self.collectionView.mj_footer endRefreshing];
  108. // [SVProgressHUD dismiss];
  109. }];
  110. }
  111. else {
  112. NSDictionary *para = @{@"sort":@"1",
  113. @"page":@(_page),
  114. @"category_id":self.cate_id,
  115. @"stype":@"0"};
  116. [KBHttp post:StocklistByCategoryId params:para success:^(id json) {
  117. if (_changeType) {
  118. [self.goodsArr removeAllObjects];
  119. }
  120. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  121. if (list.count > 0) {
  122. self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore;
  123. }else {
  124. self.collectionView.footRefreshState = MJFooterRefreshStateNoMore;
  125. }
  126. [self.goodsArr addObjectsFromArray:list];
  127. [self.collectionView reloadData];
  128. [SVProgressHUD dismiss];
  129. [self.collectionView.mj_footer endRefreshing];
  130. } failure:^(NSError *error) {
  131. [self.collectionView.mj_footer endRefreshing];
  132. [SVProgressHUD dismiss];
  133. [MBProgressHUD showMessage:@"加载失败"];
  134. }];
  135. }
  136. }
  137. #pragma mark =============== YHTypeButtonActionDelegate =============
  138. - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(NSInteger)index {
  139. _type = index + 1;
  140. _changeType = YES;
  141. [self loadCategoryGoodsList];
  142. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
  143. }
  144. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  145. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  146. {
  147. return self.goodsArr.count;
  148. }
  149. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  150. CGFloat width = (SCREEN_WIDTH-5)/2;
  151. CGFloat height = width + 102;
  152. return CGSizeMake(width, height);
  153. }
  154. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  155. {
  156. return CGSizeMake(0, 0);
  157. }
  158. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  159. {
  160. return CGSizeMake(0, 0);
  161. }
  162. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  165. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  166. cell.model = model;
  167. return cell;
  168. }
  169. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  170. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  171. if ([model.type isEqualToString:@"1"]) {
  172. //专场
  173. KBGoodListViewController *list = [[KBGoodListViewController alloc] init];
  174. list.cate_id = model.goods_id;
  175. list.topRequest = 1;
  176. [self.navigationController pushViewController:list animated:YES];
  177. }else {
  178. //详情
  179. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  180. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  181. detailVC.requestModel = requestModel;
  182. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:ListAction];
  183. detailVC.eventModel = evevtModel;
  184. [self.navigationController pushViewController:detailVC animated:YES];
  185. }
  186. }
  187. #pragma mark -------------
  188. - (NSMutableArray *)goodsArr {
  189. if (!_goodsArr) {
  190. _goodsArr = [NSMutableArray array];
  191. }
  192. return _goodsArr;
  193. }
  194. - (void)didReceiveMemoryWarning {
  195. [super didReceiveMemoryWarning];
  196. // Dispose of any resources that can be recreated.
  197. }
  198. /*
  199. #pragma mark - Navigation
  200. // In a storyboard-based application, you will often want to do a little preparation before navigation
  201. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  202. // Get the new view controller using [segue destinationViewController].
  203. // Pass the selected object to the new view controller.
  204. }
  205. */
  206. @end