// // HomeGoodBookCell.m // smartRhino // // Created by niuzhen on 2020/5/20. // Copyright © 2020 tederen. All rights reserved. // #import "HomeGoodBookCell.h" #import "BookHomeSubCell.h" #import "HomeGoodBookCollectCell.h" @implementation HomeGoodBookCell - (void)awakeFromNib { [super awakeFromNib]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.showsHorizontalScrollIndicator = NO; [self.collectionView registerNib:[UINib nibWithNibName:@"BookHomeSubCell" bundle:nil] forCellWithReuseIdentifier:@"BookHomeSubCell"]; [self.collectionView registerNib:[UINib nibWithNibName:@"HomeGoodBookCollectCell" bundle:nil] forCellWithReuseIdentifier:@"HomeGoodBookCollectCell"]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } + (HomeGoodBookCell *)configCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifer = @"HomeGoodBookCell"; HomeGoodBookCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeGoodBookCell" owner:nil options:nil] objectAtIndex:0]; } return cell; } - (void)setDataWithItem:(NSInteger)Item isUnUser:(BOOL)isUser model:(HomeSubModel *)model { self.TitleL.text = model.LabelName; self.model = model; self.type = Item % 2 == 0 ? 0 : 1; if (self.type == 0) { self.collectH.constant = 101.f * model.Items.count; UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init]; layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); [self.collectionView setCollectionViewLayout:layout]; }else{ CGFloat H = 0.f; if (model.Items.count > 3) { if (model.Items.count % 3 == 0) { H = model.Items.count / 3 * 170.f; }else{ H = model.Items.count / 3 * 170.f + 170.f; } }else{ H = 170.f; } self.collectH.constant = H; UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init]; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); [self.collectionView setCollectionViewLayout:layout]; } [self.collectionView setContentOffset:CGPointZero animated:NO]; [self.collectionView reloadData]; WS(weakSelf); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.collectionView reloadData]; }); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0.f; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return self.type == 0 ? 0.f : 15.f; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.model.Items.count; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (self.type == 0) { return CGSizeMake(SCREEN_WIDTH, 101); }else{ return CGSizeMake((SCREEN_WIDTH - 60) / 3, 170); } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary * dict = self.model.Items[indexPath.item]; HomeSubItemModel * smodel = [HomeSubItemModel modelWithDictionary:dict]; if (self.type == 0) { BookHomeSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BookHomeSubCell" forIndexPath:indexPath]; [cell setData:smodel]; return cell; }else{ HomeGoodBookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodBookCollectCell" forIndexPath:indexPath]; [cell setData:smodel]; return cell; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; NSDictionary * dict = self.model.Items[indexPath.item]; HomeSubItemModel * model = [HomeSubItemModel modelWithDictionary:dict]; if (self.ClickItemBlock) { self.ClickItemBlock(model); } } @end