// // KBMyCoupleViewController.m // YouHuiProject // // Created by 小花 on 2018/11/8. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBMyCoupleViewController.h" #import "KBNativeShopCarCollectionCell.h" #import "KBGoodDetailViewController.h" #import "KBMyCoupleHeader.h" #import "KBNoDataMsgView.h" static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell"; static NSString *MyCoupleHeader = @"MyCoupleHeader"; @interface KBMyCoupleViewController () < UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout > { NSInteger _page; ActivityIndicatorView *_indicatorView; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, strong) KBNoDataMsgView *noDataView; @end @implementation KBMyCoupleViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; } - (void)viewDidLoad { [super viewDidLoad]; _page = 1; [self initSubViews]; [self configNavigationBar]; [self requestGoods]; } - (void)configNavigationBar { [self.navigationBar setNavTitle:@"我的优惠券"]; self.navigationBar.backgroundColor = [UIColor changeColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)initSubViews { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout]; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 1; self.collectionView.showsVerticalScrollIndicator=NO; self.collectionView.showsHorizontalScrollIndicator=NO; self.collectionView.backgroundColor = [UIColor yhGrayColor]; self.collectionView.dataSource = self; self.collectionView.delegate = self; [self.collectionView registerClass:[KBNativeShopCarCollectionCell class] forCellWithReuseIdentifier:shopCarHorizontalGoodCell]; [self.collectionView registerClass:[KBMyCoupleHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MyCoupleHeader]; [self.view addSubview:self.collectionView]; MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ _page = 1; [self.collectionView.mj_footer resetNoMoreData]; [self requestGoods]; }]; refreshHeader.lastUpdatedTimeLabel.hidden=YES; [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing]; self.collectionView.mj_header = refreshHeader; self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ _page++; [self requestGoods]; }]; ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)]; _indicatorView = indicatorView; [self.collectionView addSubview:self.noDataView]; } /** 加载购物车数据 */ - (void)requestGoods { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/myCouponList",BaseURL]; [KBHttp post:url params:@{@"page":@(_page)} success:^(id json) { if ([self.collectionView.mj_header isRefreshing]) { [self.dataArray removeAllObjects]; } NSArray *list = json[@"data"]; for (NSDictionary *dict in list) { NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:dict[@"goodsList"]]; [self.dataArray addObject:arr]; } if (_page == 1 && list.count==0) { self.noDataView.hidden = NO; }else { self.noDataView.hidden = YES; } if (list.count > 0) { [self.collectionView.mj_footer endRefreshing]; }else { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } [self.collectionView.mj_header endRefreshing]; [self.collectionView reloadData]; [_indicatorView stopAnimating]; } failure:^(NSError *error) { }]; } #pragma mark --- #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSArray *array = self.dataArray[section]; return array.count; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.dataArray.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(SCREEN_WIDTH-20, 88); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 0) { return CGSizeMake(SCREEN_WIDTH-20, 45); } return CGSizeMake(SCREEN_WIDTH-20, 35); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(SCREEN_WIDTH, 10); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBNativeShopCarCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCarHorizontalGoodCell forIndexPath:indexPath]; KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; cell.model = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ KBMyCoupleHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MyCoupleHeader forIndexPath:indexPath]; KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; [header setDateWith:model.getCouponTime]; return header; } #pragma mark -内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 5, 0, 5); } #pragma mark -行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row]; KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requesModel = [[DetailRequestModel alloc] initWithChildModel:model]; detail.requestModel = requesModel; [self.navigationController pushViewController:detail animated:YES]; } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } - (KBNoDataMsgView *)noDataView { if (!_noDataView) { _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"没有找到你领取的优惠券"]; _noDataView.hidden = YES; } return _noDataView; } @end