// // KBGoosCommunityViewController.m // YouHuiProject // // Created by 小花 on 2018/11/9. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBGoosCommunityViewController.h" #import "KBNoDataMsgView.h" #import "KBGoodsCommunityCell.h" #import "KBGoodDetailViewController.h" static NSString *communityGoodCell = @"communityGoodCell"; static NSString *communityHeader = @"communityHeader"; @interface KBGoosCommunityViewController () < UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout > { NSInteger _page; ActivityIndicatorView *_indicatorView; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, strong) KBNoDataMsgView *noDataView; @end @implementation KBGoosCommunityViewController - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self initSubViews]; [self loadData]; } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; } - (void)configNavigationBar{ [self.navigationBar setNavTitle:@"优选推荐"]; self.view.backgroundColor = [UIColor whiteColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; self.navigationBar.backgroundColor = [UIColor homeRedColor]; self.navigationBar.showNavigationBarBottomLine = YES; } - (void)initSubViews { _page = 1; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight) 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:[KBGoodsCommunityCell class] forCellWithReuseIdentifier:communityGoodCell]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:communityHeader]; [self.view addSubview:self.collectionView]; MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ _page = 1; [self.collectionView.mj_footer resetNoMoreData]; [self loadData]; }]; refreshHeader.lastUpdatedTimeLabel.hidden=YES; [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing]; self.collectionView.mj_header = refreshHeader; self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ _page++; [self loadData]; }]; ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)]; _indicatorView = indicatorView; [self.collectionView addSubview:self.noDataView]; } - (void)loadData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/preference",BaseURL]; [KBHttp post:url params:@{@"page":@(_page)} success:^(id json) { if ([self.collectionView.mj_header isRefreshing]) { [self.dataArray removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; [self.dataArray addObjectsFromArray:list]; if (list.count == 0 && _page == 1) { self.noDataView.hidden = NO; }else { self.noDataView.height = YES; } if (list.count == 0) { [self.collectionView.mj_footer endRefreshingWithNoMoreData]; }else { [self.collectionView.mj_footer endRefreshing]; } [self.collectionView reloadData]; [self.collectionView.mj_header endRefreshing]; [_indicatorView stopAnimating]; } failure:^(NSError *error) { [_indicatorView stopAnimating]; }]; } #pragma mark --- #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArray.count; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(SCREEN_WIDTH-20, 185); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodsCommunityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:communityGoodCell forIndexPath:indexPath]; KBChildGoodModel *model = self.dataArray[indexPath.row]; cell.model = model; return cell; } #pragma mark -内边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 0, 0, 0); } #pragma mark -行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 10; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.dataArray[indexPath.row]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; detail.requestModel = requestModel; [self.navigationController pushViewController:detail animated:YES]; } - (KBNoDataMsgView *)noDataView { if (!_noDataView) { _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"优选圈暂时还没有任何商品"]; _noDataView.hidden = YES; } return _noDataView; } - (NSMutableArray *)dataArray { if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } @end