天天省钱快报

KBClassifyListController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // KBClassifyListController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/9.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBClassifyListController.h"
  9. #import "KBGoodCollectionCell.h"
  10. #import "KBGoodDetailViewController.h"
  11. #import "KBTypeButtonHeader.h"
  12. #import "KBGoodDetailViewController.h"
  13. #import "KBChildFilterView.h"
  14. static NSString *cellID = @"KBGoodCollectionCell";
  15. #define KHeaderBarHeight 80
  16. @interface KBClassifyListController ()
  17. <
  18. UICollectionViewDelegate,
  19. UICollectionViewDataSource,
  20. JC_SelectViewDelegate,
  21. UICollectionViewDelegateFlowLayout
  22. >
  23. {
  24. NSInteger _page;
  25. NSInteger _type;
  26. NSInteger _is_has_coupon;
  27. }
  28. @property (nonatomic, strong) UICollectionView *collectionView;
  29. @property (nonatomic, strong) NSMutableArray *goodsArr;
  30. @property (nonatomic, strong) KBChildFilterView *filterView;
  31. @end
  32. @implementation KBClassifyListController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. [self initHUD];
  36. [self configParam];
  37. [self configNavigationBar];
  38. [self configCollectionView];
  39. [self loadCategoryGoodsList:NO switchButton:nil];
  40. }
  41. #pragma mark - HUD
  42. - (void)initHUD {
  43. [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
  44. [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]];
  45. [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]];
  46. [SVProgressHUD show];
  47. }
  48. - (void)configParam {
  49. _page = 1;
  50. _type = 1; //默认请求推荐的数据
  51. _is_has_coupon = 0;
  52. }
  53. - (void)configNavigationBar {
  54. self.view.backgroundColor = [UIColor whiteColor];
  55. [self.navigationBar setNavTitle:self.name];
  56. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  57. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  58. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  59. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  60. [self.navigationBar setShowNavigationBarBottomLine:YES];
  61. }
  62. - (void)configCollectionView {
  63. self.filterView = [[KBChildFilterView alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, KHeaderBarHeight) withArr:nil];
  64. self.filterView.delegate = self;
  65. [self.view addSubview:self.filterView];
  66. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  67. flowLayout.minimumLineSpacing = 5;
  68. flowLayout.minimumInteritemSpacing = 0;
  69. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  70. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+KHeaderBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  71. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  72. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  73. self.collectionView.showsVerticalScrollIndicator = NO;
  74. self.collectionView.delegate = self;
  75. self.collectionView.dataSource = self;
  76. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  77. [self loadMoreData];
  78. }];
  79. [self.view addSubview: self.collectionView];
  80. }
  81. - (void)backAction {
  82. [self.navigationController popViewControllerAnimated:YES];
  83. }
  84. #pragma mark ----
  85. #pragma mark JC_SelectViewDelegate
  86. //仅显示优惠券
  87. - (void)selectItme:(KBChildFilterView *)selectView onlyTicketClick:(UIButton *)switchBtn {
  88. _is_has_coupon = switchBtn.selected ? 0 : 1;
  89. [self loadCategoryGoodsList:YES switchButton:switchBtn];
  90. }
  91. //上方条件选择
  92. - (void)selectTopButton:(KBChildFilterView *)selectView withIndex:(NSInteger)index withButtonType:(JCButtonClickType )type1{
  93. //价格
  94. if (index == 2&&type1) {
  95. switch (type1) {
  96. case JCButtonClickTypeNormal:
  97. //正常价格
  98. {
  99. NSLog(@"上边按钮的正常价格");
  100. }
  101. break;
  102. case JCButtonClickTypeUp:
  103. //价格升序排列
  104. {
  105. NSLog(@"上边按钮的价格升序排列");
  106. _type = 3;
  107. }
  108. break;
  109. case JCButtonClickTypeDown:
  110. //价格降序排列
  111. {
  112. NSLog(@"上边按钮的价格降序排列");
  113. _type = 4;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. }else if (index == 0){//综合
  120. NSLog(@"上边按钮的综合");
  121. _type = 1;
  122. }else if (index == 1){//销量
  123. NSLog(@"上边按钮的销量");
  124. _type = 2;
  125. }else{//筛选
  126. NSLog(@"上边按钮的筛选");
  127. }
  128. _page = 1;
  129. [self.collectionView.mj_footer resetNoMoreData];
  130. [self loadCategoryGoodsList:YES switchButton:nil];
  131. }
  132. /**
  133. 上拉加载
  134. */
  135. - (void)loadMoreData {
  136. _page++;
  137. [self loadCategoryGoodsList:NO switchButton:nil];
  138. }
  139. /**
  140. 加载下部商品列表
  141. */
  142. - (void)loadCategoryGoodsList:(BOOL)refresh switchButton:(UIButton *)switchButton{
  143. if (![self.collectionView.mj_footer isRefreshing]) {
  144. [SVProgressHUD show];
  145. }
  146. NSDictionary *para = @{@"page":@(_page),
  147. @"category_id":self.firstCategoryId,
  148. @"sort":@(_type),
  149. @"sub_category_id":self.cate_id,
  150. @"stype":@"0",
  151. @"is_has_coupon":@(_is_has_coupon)
  152. };
  153. [KBHttp post:StocklistByCategoryId params:para success:^(id json) {
  154. if (refresh) [self.goodsArr removeAllObjects];
  155. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  156. [self.goodsArr addObjectsFromArray:list];
  157. [self.collectionView reloadData];
  158. if (list.count > 0) {
  159. [self.collectionView.mj_footer endRefreshing];
  160. }else {
  161. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  162. }
  163. [SVProgressHUD dismiss];
  164. switchButton.selected = !switchButton.selected;
  165. } failure:^(NSError *error) {
  166. [self.collectionView.mj_footer endRefreshing];
  167. [SVProgressHUD dismiss];
  168. [MBProgressHUD showMessage:@"加载失败"];
  169. }];
  170. }
  171. - (void)noMoreDataWithArray:(NSArray *)array {
  172. if (array.count > 0) {
  173. self.collectionView.footRefreshState = MJFooterRefreshStateNoMore;
  174. }else {
  175. self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore;
  176. }
  177. }
  178. #pragma mark - scrollView
  179. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  180. {
  181. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  182. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  183. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  184. CGFloat velocity = [pan velocityInView:scrollView].y;
  185. if (velocity <- 10) {
  186. //向上拖动,隐藏导航栏
  187. if (self.navigationBar.y == -NavBarHeight) {
  188. return;
  189. }
  190. [UIView animateWithDuration:0.15 animations:^{
  191. self.navigationBar.y = -NavBarHeight;
  192. self.filterView.y = KStatusBarHeight;
  193. self.collectionView.y = KStatusBarHeight + KHeaderBarHeight;
  194. self.collectionView.height = SCREEN_HEIGHT-KHeaderBarHeight-KStatusBarHeight;
  195. }];
  196. }else if (velocity > 10) {
  197. //向下拖动,显示导航栏
  198. if (self.navigationBar.y == 0) {
  199. return;
  200. }
  201. [UIView animateWithDuration:0.15 animations:^{
  202. self.navigationBar.y = 0;
  203. self.filterView.y = NavBarHeight;
  204. self.collectionView.y = NavBarHeight + KHeaderBarHeight;
  205. }];
  206. }else if(velocity == 0){
  207. //停止拖拽
  208. }
  209. }
  210. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  211. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  212. {
  213. return self.goodsArr.count;
  214. }
  215. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  216. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  217. CGFloat width = (SCREEN_WIDTH-5)/2;
  218. CGFloat height = width + 102;
  219. return CGSizeMake(width, height);
  220. }
  221. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  222. {
  223. return CGSizeMake(0, 0);
  224. }
  225. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  226. {
  227. return CGSizeMake(0, 0);
  228. }
  229. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  230. {
  231. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  232. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  233. cell.model = model;
  234. return cell;
  235. }
  236. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  237. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  238. //详情
  239. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  240. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  241. detailVC.requestModel = requestModel;
  242. if (self.isOtherPage) {
  243. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:HomeClassifyAction];
  244. detailVC.eventModel = evevtModel;
  245. }else {
  246. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.cate_id source:ListClassifyAction];
  247. detailVC.eventModel = evevtModel;
  248. }
  249. [self.navigationController pushViewController:detailVC animated:YES];
  250. }
  251. #pragma mark -------------
  252. - (NSMutableArray *)goodsArr {
  253. if (!_goodsArr) {
  254. _goodsArr = [NSMutableArray array];
  255. }
  256. return _goodsArr;
  257. }
  258. - (void)didReceiveMemoryWarning {
  259. [super didReceiveMemoryWarning];
  260. // Dispose of any resources that can be recreated.
  261. }
  262. /*
  263. #pragma mark - Navigation
  264. // In a storyboard-based application, you will often want to do a little preparation before navigation
  265. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  266. // Get the new view controller using [segue destinationViewController].
  267. // Pass the selected object to the new view controller.
  268. }
  269. */
  270. @end