口袋优选

KBNativeShopCarViewController.m 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // KBNativeShopCarViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/2.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBNativeShopCarViewController.h"
  9. #import "JsonTool.h"
  10. #import "KBNativeShopCarHeader.h"
  11. #import "KBNativeShopCarCollectionCell.h"
  12. #import "KBChildGoodModel.h"
  13. #import "KBGoodDetailViewController.h"
  14. #import "KBNoDataMsgView.h"
  15. static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell";
  16. static NSString *shopCarHorizontalHeader = @"shopCarHorizontalHeader";
  17. @interface KBNativeShopCarViewController ()
  18. <
  19. UICollectionViewDelegate,
  20. UICollectionViewDataSource,
  21. UICollectionViewDelegateFlowLayout
  22. >
  23. {
  24. NSInteger _page;
  25. ActivityIndicatorView *_indicatorView;
  26. KBNativeShopCarHeader *_header;
  27. }
  28. @property (nonatomic, strong) UICollectionView *collectionView;
  29. @property (nonatomic, strong) NSMutableArray *dataArray;
  30. @property (nonatomic, strong) NSArray *goodsIdArr;
  31. @property (nonatomic, strong) KBNoDataMsgView *noDataView;
  32. @end
  33. @implementation KBNativeShopCarViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. _page = 1;
  37. [self addWebShopCarCompleteNoti];
  38. [self initSubViews];
  39. }
  40. - (void)initSubViews {
  41. KBNativeShopCarHeader *header = [[KBNativeShopCarHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
  42. _header = header;
  43. [self.view addSubview:header];
  44. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  45. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, header.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight-header.height) collectionViewLayout:flowLayout];
  46. flowLayout.minimumLineSpacing = 5;
  47. flowLayout.minimumInteritemSpacing = 1;
  48. self.collectionView.showsVerticalScrollIndicator=NO;
  49. self.collectionView.showsHorizontalScrollIndicator=NO;
  50. self.collectionView.backgroundColor = [UIColor yhGrayColor];
  51. self.collectionView.dataSource = self;
  52. self.collectionView.delegate = self;
  53. [self.collectionView registerClass:[KBNativeShopCarCollectionCell class] forCellWithReuseIdentifier:shopCarHorizontalGoodCell];
  54. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader];
  55. [self.view addSubview:self.collectionView];
  56. MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  57. _page = 1;
  58. [self.collectionView.mj_footer resetNoMoreData];
  59. [self requestGoods:YES];
  60. }];
  61. refreshHeader.lastUpdatedTimeLabel.hidden=YES;
  62. [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
  63. self.collectionView.mj_header = refreshHeader;
  64. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  65. _page++;
  66. [self requestGoods:NO];
  67. }];
  68. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  69. _indicatorView = indicatorView;
  70. [self.collectionView addSubview:self.noDataView];
  71. }
  72. /**
  73. 监听web购物车是否加载完成
  74. */
  75. - (void)addWebShopCarCompleteNoti {
  76. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadShopCarGoods:) name:ShopCarCompleteKey object:nil];
  77. }
  78. - (void)loadShopCarGoods:(NSNotification *)noti {
  79. NSDictionary *dic = noti.userInfo;
  80. NSArray *goodArr = dic[@"goodsIdStr"];
  81. self.goodsIdArr = goodArr;
  82. [self requestGoods:YES];
  83. }
  84. /**
  85. 加载购物车数据
  86. */
  87. - (void)requestGoods:(BOOL)refresh {
  88. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/cartGoods",BaseURL];
  89. NSString *goodsStr = [JsonTool arrayToJSONString:self.goodsIdArr];
  90. NSDictionary *para = @{@"goodsIdStr":goodsStr,
  91. @"page":@(_page)
  92. };
  93. [KBHttp post:url params:para success:^(id json) {
  94. if (refresh) {
  95. [self.dataArray removeAllObjects];
  96. }
  97. NSArray *list = json[@"data"];
  98. for (NSDictionary *dict in list) {
  99. NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:dict[@"goodsList"]];
  100. [self.dataArray addObject:arr];
  101. }
  102. [_header setDataWithDic:json];
  103. if (list.count > 0) {
  104. [self.collectionView.mj_footer endRefreshing];
  105. }else {
  106. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  107. }
  108. if (_page == 1 && list.count == 0 ) {
  109. self.noDataView.hidden = NO;
  110. }else {
  111. self.noDataView.hidden = YES;
  112. }
  113. [self.collectionView.mj_header endRefreshing];
  114. [self.collectionView reloadData];
  115. [_indicatorView stopAnimating];
  116. } failure:^(NSError *error) {
  117. }];
  118. }
  119. #pragma mark ---
  120. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  121. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  122. {
  123. NSArray *array = self.dataArray[section];
  124. return array.count;
  125. }
  126. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  127. return self.dataArray.count;
  128. }
  129. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  130. return CGSizeMake(SCREEN_WIDTH-20, 88);
  131. }
  132. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  133. {
  134. if (section == 0) {
  135. return CGSizeMake(SCREEN_WIDTH-20, 45);
  136. }
  137. return CGSizeMake(SCREEN_WIDTH-20, 35);
  138. }
  139. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  140. {
  141. return CGSizeMake(SCREEN_WIDTH, 10);
  142. }
  143. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. KBNativeShopCarCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCarHorizontalGoodCell forIndexPath:indexPath];
  146. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  147. cell.model = model;
  148. return cell;
  149. }
  150. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  151. UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader forIndexPath:indexPath];
  152. [header removeAllSubviews];
  153. UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 35)];
  154. bg.backgroundColor = [UIColor whiteColor];
  155. UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 16, 16)];
  156. [bg addSubview:imgView];
  157. imgView.centerY = bg.height/2;
  158. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  159. NSString *imgName = [model.shop_type boolValue]?@"cl-tm":@"taobao";
  160. imgView.image = [UIImage imageNamed:imgName];
  161. UILabel *nick = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
  162. nick.font = [UIFont systemFontOfSize:13];
  163. nick.text = model.nick;
  164. nick.centerY = bg.height/2;
  165. [bg addSubview:nick];
  166. nick.left = imgView.right+5;
  167. [header addSubview:bg];
  168. bg.bottom = header.height;
  169. return header;
  170. }
  171. #pragma mark -内边距
  172. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  173. return UIEdgeInsetsMake(0, 5, 0, 5);
  174. }
  175. #pragma mark -行间距
  176. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  177. return 0;
  178. }
  179. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  180. KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  181. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:ShoppingAction];
  182. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  183. detail.eventModel = evevtModel;
  184. DetailRequestModel *requesModel = [[DetailRequestModel alloc] initWithChildModel:model];
  185. detail.requestModel = requesModel;
  186. [self.navigationController pushViewController:detail animated:YES];
  187. }
  188. - (NSMutableArray *)dataArray {
  189. if (!_dataArray) {
  190. _dataArray = [NSMutableArray array];
  191. }
  192. return _dataArray;
  193. }
  194. - (KBNoDataMsgView *)noDataView {
  195. if (!_noDataView) {
  196. _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"没有发现购物车里的商品"];
  197. _noDataView.hidden = YES;
  198. }
  199. return _noDataView;
  200. }
  201. @end