// // LDSimilarGoodsController.m // YouHuiProject // // Created by 小花 on 2018/2/2. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDSimilarGoodsController.h" #import "LDGoodCollectionCell.h" static NSString *cellID = @"LDGoodCollectionCell"; @interface LDSimilarGoodsController () { } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *goodsArr; @end @implementation LDSimilarGoodsController - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self configCollectionView]; [self loadCategoryGoodsList]; } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; [self.navigationBar setNavTitle:@"相似推荐"]; self.navigationBar.showNavigationBarBottomLine = YES; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; [self.navigationBar setShowNavigationBarBottomLine:YES]; } - (void)configCollectionView { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; CGFloat width = (SCREEN_WIDTH-5)/2; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout]; [self.collectionView registerClass:[LDGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6]; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.view addSubview: self.collectionView]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } /** 加载下部商品列表 */ - (void)loadCategoryGoodsList { NSDictionary *para = @{@"goods_id":self.goods_id}; [LDHttp post:SimilarGoods params:para success:^(id json) { NSArray *list = [NSArray yy_modelArrayWithClass:[LDChildGoodModel class] json:json]; [self.goodsArr addObjectsFromArray:list]; [self.collectionView reloadData]; if (self.goodsArr.count == 0) { [SVProgressHUD showInfoWithStatus:@"暂无数据"]; } } failure:^(NSError *error) { [SVProgressHUD showInfoWithStatus:@"暂无数据"]; }]; } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.goodsArr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { LDChildGoodModel *model = self.goodsArr[indexPath.row]; CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 135; if (![AccountTool isLogin] && ![model.red_active_status boolValue]) { height = width +112; } return CGSizeMake(width, height); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { LDGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; LDChildGoodModel *model = self.goodsArr[indexPath.row]; cell.model = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { LDChildGoodModel *model = self.goodsArr[indexPath.row]; //详情 LDGoodDetailViewController *detailVC = [[LDGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; LDEventModel *evevtModel = [[LDEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:sameCouponAction]; detailVC.eventModel = evevtModel; [self.navigationController pushViewController:detailVC animated:YES]; } #pragma mark ------------- - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end