口袋优选

KBGoodListCidViewController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // KBGoodListCidViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/2/2.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBGoodListCidViewController.h"
  9. #import "KBGoodListViewController.h"
  10. #import "KBGoodDetailViewController.h"
  11. #import "KBGoodCollectionCell.h"
  12. #import "KBOnePicCell.h"
  13. #define BottomViewHeight self.bottomGoodsArr.count*FITSIZE(162)+FITSIZE(40)
  14. static NSString *const collectionViewCellID = @"KBGoodCollectionCell";
  15. static NSString *const collectionViewBottom = @"YHGoodListBottomView";
  16. static NSString *const tableViewCellID = @"KBOnePicCell";
  17. @interface YHGoodListBottomView () <UITableViewDelegate,UITableViewDataSource>
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @end
  20. @implementation YHGoodListBottomView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.backgroundColor = [UIColor clearColor];
  25. [self initSubviews];
  26. }
  27. return self;
  28. }
  29. - (void)initSubviews {
  30. [self addSubview:self.tableView];
  31. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, FITSIZE(40))];
  32. topView.backgroundColor = [UIColor clearColor];
  33. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(topView.width/2-FITSIZE(152)/2, topView.height/2-FITSIZE(12)/2, FITSIZE(152), FITSIZE(12))];
  34. imageView.backgroundColor = [UIColor clearColor];
  35. imageView.image = [UIImage imageNamed:@"special_recommendation"];
  36. [topView addSubview:imageView];
  37. self.tableView.tableHeaderView = topView;
  38. }
  39. - (void)setGoodsArr:(NSMutableArray *)goodsArr {
  40. _goodsArr = goodsArr;
  41. [self.tableView reloadData];
  42. }
  43. #pragma mark - tableView
  44. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  45. return 1;
  46. }
  47. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  48. return self.goodsArr.count;
  49. }
  50. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  51. KBOnePicCell *cell = [KBOnePicCell cellWithTableView:tableView];
  52. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  53. cell.model = model;
  54. return cell;
  55. }
  56. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  57. if ([self.delegate respondsToSelector:@selector(yh_GoodListBottomViewDidSelectItemAt:)]) {
  58. [self.delegate yh_GoodListBottomViewDidSelectItemAt:indexPath];
  59. }
  60. }
  61. #pragma mark - lazy
  62. - (UITableView *)tableView {
  63. if (!_tableView) {
  64. _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
  65. _tableView.backgroundView.backgroundColor = [UIColor clearColor];
  66. _tableView.backgroundColor = [UIColor clearColor];
  67. _tableView.delegate = self;
  68. _tableView.dataSource = self;
  69. _tableView.scrollEnabled = NO;
  70. _tableView.bounces = NO;
  71. _tableView.showsVerticalScrollIndicator = NO;
  72. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  73. _tableView.rowHeight = FITSIZE(162);
  74. _tableView.estimatedRowHeight = 0;
  75. _tableView.estimatedSectionHeaderHeight = 0;
  76. _tableView.estimatedSectionFooterHeight = 0;
  77. [_tableView registerClass:[KBOnePicCell class] forCellReuseIdentifier:tableViewCellID];
  78. }
  79. return _tableView;
  80. }
  81. @end
  82. @interface KBGoodListCidViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,YHGoodListBottomViewDelegate>
  83. @property (nonatomic, strong) UICollectionView *collectionView;
  84. @property (nonatomic, strong) NSMutableArray *topGoodsArr;
  85. @property (nonatomic, strong) NSMutableArray *bottomGoodsArr;
  86. @end
  87. @implementation KBGoodListCidViewController
  88. - (void)viewDidLoad {
  89. [super viewDidLoad];
  90. [self initHUD];
  91. [self initNavBar];
  92. [self requestCategoryGoods];
  93. }
  94. - (void)viewWillDisappear:(BOOL)animated {
  95. [super viewWillDisappear:animated];
  96. [SVProgressHUD dismiss];
  97. }
  98. - (void)didReceiveMemoryWarning {
  99. [super didReceiveMemoryWarning];
  100. }
  101. - (void)initNavBar {
  102. [self.navigationBar setBackButtonWithTarget:self selector:@selector(backAction)];
  103. [self.navigationBar setShowNavigationBarBottomLine:YES];
  104. [self.navigationBar setNavTitle:@"专场"];
  105. }
  106. - (void)backAction {
  107. [self.navigationController popViewControllerAnimated:YES];
  108. }
  109. - (void)initSubviews {
  110. [SVProgressHUD dismiss];
  111. [self.view addSubview:self.collectionView];
  112. }
  113. #pragma mark - HUD
  114. - (void)initHUD {
  115. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
  116. [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]];
  117. [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]];
  118. }
  119. #pragma mark - request
  120. - (void)requestCategoryGoods {
  121. [SVProgressHUD show];
  122. if (self.topRequest == 1) {
  123. [KBHttp post:SimilarGoods params:@{@"goods_id":self.goods_id} success:^(id json) {
  124. NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json];
  125. if (arr.count > 0) {
  126. [self.topGoodsArr addObjectsFromArray:arr];
  127. [self requestRelevantRecommendations];
  128. }
  129. else {
  130. [self initSubviews];
  131. }
  132. } failure:^(NSError *error) {
  133. [SVProgressHUD dismiss];
  134. }];
  135. }
  136. else {
  137. [KBHttp post:GoodlistByGid params:@{@"gid":self.goods_id} success:^(id json) {
  138. NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  139. if (arr.count > 0) {
  140. [self.topGoodsArr addObjectsFromArray:arr];
  141. [self requestRelevantRecommendations];
  142. }
  143. else {
  144. [self initSubviews];
  145. }
  146. } failure:^(NSError *error) {
  147. [SVProgressHUD dismiss];
  148. }];
  149. }
  150. }
  151. - (void)requestRelevantRecommendations {
  152. [KBHttp post:RelevantRecommendations params:@{@"page":@1,@"cid":self.cid} success:^(id json) {
  153. NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  154. if (arr.count > 0) {
  155. [self.bottomGoodsArr addObjectsFromArray:arr];
  156. }
  157. [self initSubviews];
  158. } failure:^(NSError *error) {
  159. [SVProgressHUD dismiss];
  160. }];
  161. }
  162. #pragma mark - goodListBottomView
  163. - (void)yh_GoodListBottomViewDidSelectItemAt:(NSIndexPath *)indexPath {
  164. KBChildGoodModel *model = self.bottomGoodsArr[indexPath.row];
  165. if ([model.type isEqualToString:@"0"]) {
  166. //详情
  167. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  168. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  169. detailVC.requestModel = requestModel;
  170. [self.navigationController pushViewController:detailVC animated:YES];
  171. }
  172. else {
  173. //专场
  174. KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init];
  175. listVC.cate_id = model.goods_id;
  176. [self.navigationController pushViewController:listVC animated:YES];
  177. }
  178. }
  179. #pragma mark - collectionView
  180. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  181. return 1;
  182. }
  183. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  184. return self.topGoodsArr.count;
  185. }
  186. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  187. KBGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellID forIndexPath:indexPath];
  188. cell.backgroundColor = [UIColor whiteColor];
  189. cell.backgroundView.backgroundColor = [UIColor whiteColor];
  190. cell.contentView.backgroundColor = [UIColor whiteColor];
  191. KBChildGoodModel *model = self.topGoodsArr[indexPath.item];
  192. cell.model = model;
  193. return cell;
  194. }
  195. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  196. YHGoodListBottomView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:collectionViewBottom forIndexPath:indexPath];
  197. view.delegate = self;
  198. view.goodsArr = self.bottomGoodsArr;
  199. return view;
  200. }
  201. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  202. return CGSizeMake(kScreenWidth, BottomViewHeight);
  203. }
  204. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  205. KBChildGoodModel *model = self.topGoodsArr[indexPath.item];
  206. if ([model.type isEqualToString:@"0"]) {
  207. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  208. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  209. detailVC.requestModel = requestModel;
  210. [self.navigationController pushViewController:detailVC animated:YES];
  211. }
  212. else {
  213. KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init];
  214. listVC.cate_id = model.goods_id;
  215. [self.navigationController pushViewController:listVC animated:YES];
  216. }
  217. }
  218. #pragma mark - lazy
  219. - (UICollectionView *)collectionView {
  220. if (!_collectionView) {
  221. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  222. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  223. flowLayout.itemSize = CGSizeMake(FITSIZE(184.9), FITSIZE(287));
  224. flowLayout.minimumLineSpacing = FITSIZE(5);
  225. flowLayout.minimumInteritemSpacing = FITSIZE(5);
  226. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NavBarHeight, kScreenWidth, kScreenHeight-NavBarHeight) collectionViewLayout:flowLayout];
  227. _collectionView.backgroundView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  228. _collectionView.backgroundColor = [UIColor clearColor];
  229. _collectionView.delegate = self;
  230. _collectionView.dataSource = self;
  231. [_collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:collectionViewCellID];
  232. [_collectionView registerClass:[YHGoodListBottomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:collectionViewBottom];
  233. if (@available(iOS 11.0, *)) {
  234. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  235. }
  236. }
  237. return _collectionView;
  238. }
  239. - (NSMutableArray *)topGoodsArr {
  240. if (!_topGoodsArr) {
  241. _topGoodsArr = [NSMutableArray array];
  242. }
  243. return _topGoodsArr;
  244. }
  245. - (NSMutableArray *)bottomGoodsArr {
  246. if (!_bottomGoodsArr) {
  247. _bottomGoodsArr = [NSMutableArray array];
  248. }
  249. return _bottomGoodsArr;
  250. }
  251. @end