口袋优选

KBMyCoupleViewController.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // KBMyCoupleViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/8.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBMyCoupleViewController.h"
  9. #import "KBNativeShopCarCollectionCell.h"
  10. #import "KBGoodDetailViewController.h"
  11. #import "KBMyCoupleHeader.h"
  12. #import "KBNoDataMsgView.h"
  13. static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell";
  14. static NSString *MyCoupleHeader = @"MyCoupleHeader";
  15. @interface KBMyCoupleViewController ()
  16. <
  17. UICollectionViewDelegate,
  18. UICollectionViewDataSource,
  19. UICollectionViewDelegateFlowLayout
  20. >
  21. {
  22. NSInteger _page;
  23. ActivityIndicatorView *_indicatorView;
  24. }
  25. @property (nonatomic, strong) UICollectionView *collectionView;
  26. @property (nonatomic, strong) NSMutableArray *dataArray;
  27. @property (nonatomic, strong) KBNoDataMsgView *noDataView;
  28. @end
  29. @implementation KBMyCoupleViewController
  30. - (void)viewWillAppear:(BOOL)animated {
  31. [super viewWillAppear:animated];
  32. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. _page = 1;
  37. [self initSubViews];
  38. [self configNavigationBar];
  39. [self requestGoods];
  40. }
  41. - (void)configNavigationBar {
  42. [self.navigationBar setNavTitle:@"我的优惠券"];
  43. self.navigationBar.backgroundColor = [UIColor changeColor];
  44. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  45. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  46. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  47. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  48. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  49. }
  50. - (void)backAction {
  51. [self.navigationController popViewControllerAnimated:YES];
  52. }
  53. - (void)initSubViews {
  54. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  55. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  56. flowLayout.minimumLineSpacing = 5;
  57. flowLayout.minimumInteritemSpacing = 1;
  58. self.collectionView.showsVerticalScrollIndicator=NO;
  59. self.collectionView.showsHorizontalScrollIndicator=NO;
  60. self.collectionView.backgroundColor = [UIColor yhGrayColor];
  61. self.collectionView.dataSource = self;
  62. self.collectionView.delegate = self;
  63. [self.collectionView registerClass:[KBNativeShopCarCollectionCell class] forCellWithReuseIdentifier:shopCarHorizontalGoodCell];
  64. [self.collectionView registerClass:[KBMyCoupleHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MyCoupleHeader];
  65. [self.view addSubview:self.collectionView];
  66. MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  67. _page = 1;
  68. [self.collectionView.mj_footer resetNoMoreData];
  69. [self requestGoods];
  70. }];
  71. refreshHeader.lastUpdatedTimeLabel.hidden=YES;
  72. [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
  73. self.collectionView.mj_header = refreshHeader;
  74. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  75. _page++;
  76. [self requestGoods];
  77. }];
  78. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  79. _indicatorView = indicatorView;
  80. [self.collectionView addSubview:self.noDataView];
  81. }
  82. /**
  83. 加载购物车数据
  84. */
  85. - (void)requestGoods {
  86. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/myCouponList",BaseURL];
  87. [KBHttp post:url params:@{@"page":@(_page)} success:^(id json) {
  88. if ([self.collectionView.mj_header isRefreshing]) {
  89. [self.dataArray removeAllObjects];
  90. }
  91. NSArray *list = json[@"data"];
  92. for (NSDictionary *dict in list) {
  93. NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:dict[@"goodsList"]];
  94. [self.dataArray addObject:arr];
  95. }
  96. if (_page == 1 && list.count==0) {
  97. self.noDataView.hidden = NO;
  98. }else {
  99. self.noDataView.hidden = YES;
  100. }
  101. if (list.count > 0) {
  102. [self.collectionView.mj_footer endRefreshing];
  103. }else {
  104. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  105. }
  106. [self.collectionView.mj_header endRefreshing];
  107. [self.collectionView reloadData];
  108. [_indicatorView stopAnimating];
  109. } failure:^(NSError *error) {
  110. }];
  111. }
  112. #pragma mark ---
  113. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  114. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  115. {
  116. NSArray *array = self.dataArray[section];
  117. return array.count;
  118. }
  119. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  120. return self.dataArray.count;
  121. }
  122. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  123. return CGSizeMake(SCREEN_WIDTH-20, 88);
  124. }
  125. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  126. {
  127. if (section == 0) {
  128. return CGSizeMake(SCREEN_WIDTH-20, 45);
  129. }
  130. return CGSizeMake(SCREEN_WIDTH-20, 35);
  131. }
  132. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  133. {
  134. return CGSizeMake(SCREEN_WIDTH, 10);
  135. }
  136. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. KBNativeShopCarCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCarHorizontalGoodCell forIndexPath:indexPath];
  139. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  140. cell.model = model;
  141. return cell;
  142. }
  143. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  144. KBMyCoupleHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MyCoupleHeader forIndexPath:indexPath];
  145. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  146. [header setDateWith:model.getCouponTime];
  147. return header;
  148. }
  149. #pragma mark -内边距
  150. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  151. return UIEdgeInsetsMake(0, 5, 0, 5);
  152. }
  153. #pragma mark -行间距
  154. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  155. return 0;
  156. }
  157. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  158. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  159. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  160. DetailRequestModel *requesModel = [[DetailRequestModel alloc] initWithChildModel:model];
  161. detail.requestModel = requesModel;
  162. [self.navigationController pushViewController:detail animated:YES];
  163. }
  164. - (NSMutableArray *)dataArray {
  165. if (!_dataArray) {
  166. _dataArray = [NSMutableArray array];
  167. }
  168. return _dataArray;
  169. }
  170. - (KBNoDataMsgView *)noDataView {
  171. if (!_noDataView) {
  172. _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"没有找到你领取的优惠券"];
  173. _noDataView.hidden = YES;
  174. }
  175. return _noDataView;
  176. }
  177. @end