口袋优选

KBGoosCommunityViewController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // KBGoosCommunityViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBGoosCommunityViewController.h"
  9. #import "KBNoDataMsgView.h"
  10. #import "KBGoodsCommunityCell.h"
  11. #import "KBGoodDetailViewController.h"
  12. static NSString *communityGoodCell = @"communityGoodCell";
  13. static NSString *communityHeader = @"communityHeader";
  14. @interface KBGoosCommunityViewController ()
  15. <
  16. UICollectionViewDelegate,
  17. UICollectionViewDataSource,
  18. UICollectionViewDelegateFlowLayout
  19. >
  20. {
  21. NSInteger _page;
  22. ActivityIndicatorView *_indicatorView;
  23. }
  24. @property (nonatomic, strong) UICollectionView *collectionView;
  25. @property (nonatomic, strong) NSMutableArray *dataArray;
  26. @property (nonatomic, strong) KBNoDataMsgView *noDataView;
  27. @end
  28. @implementation KBGoosCommunityViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. [self configNavigationBar];
  32. [self initSubViews];
  33. [self loadData];
  34. }
  35. -(void)viewDidAppear:(BOOL)animated{
  36. [super viewDidAppear:animated];
  37. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  38. }
  39. - (void)configNavigationBar{
  40. [self.navigationBar setNavTitle:@"优选推荐"];
  41. self.view.backgroundColor = [UIColor whiteColor];
  42. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  43. self.navigationBar.backgroundColor = [UIColor homeRedColor];
  44. self.navigationBar.showNavigationBarBottomLine = YES;
  45. }
  46. - (void)initSubViews {
  47. _page = 1;
  48. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  49. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight) collectionViewLayout:flowLayout];
  50. flowLayout.minimumLineSpacing = 5;
  51. flowLayout.minimumInteritemSpacing = 1;
  52. self.collectionView.showsVerticalScrollIndicator=NO;
  53. self.collectionView.showsHorizontalScrollIndicator=NO;
  54. self.collectionView.backgroundColor = [UIColor yhGrayColor];
  55. self.collectionView.dataSource = self;
  56. self.collectionView.delegate = self;
  57. [self.collectionView registerClass:[KBGoodsCommunityCell class] forCellWithReuseIdentifier:communityGoodCell];
  58. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:communityHeader];
  59. [self.view addSubview:self.collectionView];
  60. MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  61. _page = 1;
  62. [self.collectionView.mj_footer resetNoMoreData];
  63. [self loadData];
  64. }];
  65. refreshHeader.lastUpdatedTimeLabel.hidden=YES;
  66. [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
  67. self.collectionView.mj_header = refreshHeader;
  68. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  69. _page++;
  70. [self loadData];
  71. }];
  72. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  73. _indicatorView = indicatorView;
  74. [self.collectionView addSubview:self.noDataView];
  75. }
  76. - (void)loadData {
  77. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/preference",BaseURL];
  78. [KBHttp post:url params:@{@"page":@(_page)} success:^(id json) {
  79. if ([self.collectionView.mj_header isRefreshing]) {
  80. [self.dataArray removeAllObjects];
  81. }
  82. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  83. [self.dataArray addObjectsFromArray:list];
  84. if (list.count == 0 && _page == 1) {
  85. self.noDataView.hidden = NO;
  86. }else {
  87. self.noDataView.height = YES;
  88. }
  89. if (list.count == 0) {
  90. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  91. }else {
  92. [self.collectionView.mj_footer endRefreshing];
  93. }
  94. [self.collectionView reloadData];
  95. [self.collectionView.mj_header endRefreshing];
  96. [_indicatorView stopAnimating];
  97. } failure:^(NSError *error) {
  98. [_indicatorView stopAnimating];
  99. }];
  100. }
  101. #pragma mark ---
  102. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  103. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  104. {
  105. return self.dataArray.count;
  106. }
  107. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  108. return 1;
  109. }
  110. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  111. return CGSizeMake(SCREEN_WIDTH-20, 185);
  112. }
  113. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. KBGoodsCommunityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:communityGoodCell forIndexPath:indexPath];
  116. KBChildGoodModel *model = self.dataArray[indexPath.row];
  117. cell.model = model;
  118. return cell;
  119. }
  120. #pragma mark -内边距
  121. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  122. return UIEdgeInsetsMake(10, 0, 0, 0);
  123. }
  124. #pragma mark -行间距
  125. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  126. return 10;
  127. }
  128. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  129. KBChildGoodModel *model = self.dataArray[indexPath.row];
  130. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  131. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  132. detail.requestModel = requestModel;
  133. [self.navigationController pushViewController:detail animated:YES];
  134. }
  135. - (KBNoDataMsgView *)noDataView {
  136. if (!_noDataView) {
  137. _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"优选圈暂时还没有任何商品"];
  138. _noDataView.hidden = YES;
  139. }
  140. return _noDataView;
  141. }
  142. - (NSMutableArray *)dataArray {
  143. if (!_dataArray) {
  144. _dataArray = [NSMutableArray array];
  145. }
  146. return _dataArray;
  147. }
  148. @end