// // KBTodayModelView2.m // YouHuiProject // // Created by jcymac on 2018/9/4. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBTodayModelView2.h" #import "KBModelCollectionCell.h" #import "KBGoodDetailViewController.h" static NSString *KModelViewCell = @"modelViewCell"; @interface KBTodayModelView2 () { NSArray *dataArr; NSInteger _num; } @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation KBTodayModelView2 - (void)setGoodData:(NSArray *)data { dataArr = data.mutableCopy; [self.collectionView reloadData]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _num = 1; self.layer.masksToBounds = YES; [self initSubView]; } return self; } - (instancetype)initWithFrame:(CGRect)frame titleNumOfLine:(NSInteger)num{ self = [super initWithFrame:frame]; if (self) { _num = num; self.layer.masksToBounds = YES; [self initSubView]; } return self; } - (void)initSubView { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout]; flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 0; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView.backgroundColor = [UIColor clearColor]; [self.collectionView registerClass:[KBModelCollectionCell class] forCellWithReuseIdentifier:KModelViewCell]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.showsHorizontalScrollIndicator = NO; [self addSubview:self.collectionView]; } #pragma mark ---- UICollectionView Delegate ------ -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((self.width-Fitsize(20))/3.0f, self.collectionView.height); } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return dataArr.count; } - (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 { KBChildGoodModel *model = dataArr[indexPath.row]; KBModelCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:KModelViewCell forIndexPath:indexPath]; cell.model = model; cell.titleLabel.numberOfLines = _num; return cell; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 5, 0, 0); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = dataArr[indexPath.row]; KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *request = [[DetailRequestModel alloc] initWithChildModel:model]; detail.requestModel = request; if (self.isOhterPage) { KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.Id source:CategoryRecommendAction]; detail.eventModel = evevtModel; }else { KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.Id source:AdvFourListGoodsAction]; detail.eventModel = evevtModel; } [[self currentViewController].navigationController pushViewController:detail animated:YES]; if (self.isOhterPage) { [MobClick event:CategoryRecommend label:self.name]; }else { [MobClick event:TodayClickGoods label:self.name]; } } - (UIViewController *)currentViewController{ UIViewController * currVC = nil; UIWindow *window = [UIApplication sharedApplication].delegate.window; UIViewController * Rootvc = window.rootViewController; do { if ([Rootvc isKindOfClass:[UINavigationController class]]) { UINavigationController * nav = (UINavigationController *)Rootvc; UIViewController * v = [nav.viewControllers lastObject]; currVC = v; Rootvc = v.presentedViewController; continue; }else if([Rootvc isKindOfClass:[UITabBarController class]]){ UITabBarController * tabVC = (UITabBarController *)Rootvc; currVC = tabVC; Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex]; continue; } } while (Rootvc!=nil); return currVC; } @end