123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- //
- // KBNativeShopCarViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/11/2.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBNativeShopCarViewController.h"
- #import "JsonTool.h"
- #import "KBNativeShopCarHeader.h"
- #import "KBNativeShopCarCollectionCell.h"
- #import "KBChildGoodModel.h"
- #import "KBGoodDetailViewController.h"
- #import "KBNoDataMsgView.h"
- static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell";
- static NSString *shopCarHorizontalHeader = @"shopCarHorizontalHeader";
- @interface KBNativeShopCarViewController ()
- <
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- UICollectionViewDelegateFlowLayout
- >
- {
- NSInteger _page;
- ActivityIndicatorView *_indicatorView;
- KBNativeShopCarHeader *_header;
- }
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *dataArray;
- @property (nonatomic, strong) NSArray *goodsIdArr;
- @property (nonatomic, strong) KBNoDataMsgView *noDataView;
- @end
- @implementation KBNativeShopCarViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- _page = 1;
- [self addWebShopCarCompleteNoti];
- [self initSubViews];
- }
- - (void)initSubViews {
- KBNativeShopCarHeader *header = [[KBNativeShopCarHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
- _header = header;
- [self.view addSubview:header];
-
- UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
- self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, header.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight-header.height) 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:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader];
- [self.view addSubview:self.collectionView];
-
- MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- _page = 1;
- [self.collectionView.mj_footer resetNoMoreData];
- [self requestGoods:YES];
- }];
- refreshHeader.lastUpdatedTimeLabel.hidden=YES;
- [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
- self.collectionView.mj_header = refreshHeader;
-
- self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
- _page++;
- [self requestGoods:NO];
- }];
-
- ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
- _indicatorView = indicatorView;
-
- [self.collectionView addSubview:self.noDataView];
- }
- /**
- 监听web购物车是否加载完成
- */
- - (void)addWebShopCarCompleteNoti {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadShopCarGoods:) name:ShopCarCompleteKey object:nil];
- }
- - (void)loadShopCarGoods:(NSNotification *)noti {
- NSDictionary *dic = noti.userInfo;
- NSArray *goodArr = dic[@"goodsIdStr"];
- self.goodsIdArr = goodArr;
- [self requestGoods:YES];
- }
- /**
- 加载购物车数据
- */
- - (void)requestGoods:(BOOL)refresh {
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/cartGoods",BaseURL];
- NSString *goodsStr = [JsonTool arrayToJSONString:self.goodsIdArr];
- NSDictionary *para = @{@"goodsIdStr":goodsStr,
- @"page":@(_page)
- };
- [KBHttp post:url params:para success:^(id json) {
- if (refresh) {
- [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];
- }
- [_header setDataWithDic:json];
-
- if (list.count > 0) {
- [self.collectionView.mj_footer endRefreshing];
- }else {
- [self.collectionView.mj_footer endRefreshingWithNoMoreData];
- }
- if (_page == 1 && list.count == 0 ) {
- self.noDataView.hidden = NO;
- }else {
- self.noDataView.hidden = YES;
- }
-
- [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{
- UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader forIndexPath:indexPath];
- [header removeAllSubviews];
- UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 35)];
- bg.backgroundColor = [UIColor whiteColor];
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 16, 16)];
- [bg addSubview:imgView];
- imgView.centerY = bg.height/2;
- KBChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
- NSString *imgName = [model.shop_type boolValue]?@"cl-tm":@"taobao";
- imgView.image = [UIImage imageNamed:imgName];
- UILabel *nick = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
- nick.font = [UIFont systemFontOfSize:13];
- nick.text = model.nick;
- nick.centerY = bg.height/2;
- [bg addSubview:nick];
- nick.left = imgView.right+5;
- [header addSubview:bg];
- bg.bottom = header.height;
- 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];
- KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:ShoppingAction];
- KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
- detail.eventModel = evevtModel;
- 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
|