口袋优选

KBTodayModelView.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // KBTodayModelView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/11.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBTodayModelView.h"
  9. #import "KBModelCollectionCell.h"
  10. #import "KBGoodDetailViewController.h"
  11. static NSString *KModelViewCell = @"modelViewCell";
  12. @interface KBTodayModelView ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  13. {
  14. NSArray *dataArr;
  15. NSInteger _num;
  16. }
  17. @property (nonatomic, strong) UICollectionView *collectionView;
  18. @end
  19. @implementation KBTodayModelView
  20. - (void)setGoodData:(NSArray *)data {
  21. dataArr = data.mutableCopy;
  22. [self.collectionView reloadData];
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. _num = 1;
  28. self.layer.masksToBounds = YES;
  29. [self initSubView];
  30. }
  31. return self;
  32. }
  33. - (instancetype)initWithFrame:(CGRect)frame titleNumOfLine:(NSInteger)num{
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. _num = num;
  37. self.layer.masksToBounds = YES;
  38. [self initSubView];
  39. }
  40. return self;
  41. }
  42. - (void)initSubView {
  43. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  44. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:flowLayout];
  45. flowLayout.minimumLineSpacing = 5;
  46. flowLayout.minimumInteritemSpacing = 0;
  47. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  48. self.collectionView.backgroundColor = [UIColor clearColor];
  49. [self.collectionView registerClass:[KBModelCollectionCell class] forCellWithReuseIdentifier:KModelViewCell];
  50. self.collectionView.delegate = self;
  51. self.collectionView.dataSource = self;
  52. self.collectionView.showsHorizontalScrollIndicator = NO;
  53. [self addSubview:self.collectionView];
  54. }
  55. #pragma mark ---- UICollectionView Delegate ------
  56. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  57. return CGSizeMake((self.width-Fitsize(20))/3.0f, self.collectionView.height);
  58. }
  59. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  60. return 1;
  61. }
  62. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  63. return dataArr.count;
  64. }
  65. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  66. {
  67. return CGSizeMake(0, 0);
  68. }
  69. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  70. {
  71. return CGSizeMake(0, 0);
  72. }
  73. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  74. KBChildGoodModel *model = dataArr[indexPath.row];
  75. KBModelCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:KModelViewCell forIndexPath:indexPath];
  76. cell.model = model;
  77. cell.titleLabel.numberOfLines = _num;
  78. return cell;
  79. }
  80. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  81. return UIEdgeInsetsMake(0, 5, 0, 0);
  82. }
  83. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  84. KBChildGoodModel *model = dataArr[indexPath.row];
  85. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  86. DetailRequestModel *request = [[DetailRequestModel alloc] initWithChildModel:model];
  87. detail.requestModel = request;
  88. if (self.isOhterPage) {
  89. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.Id source:CategoryRecommendAction];
  90. detail.eventModel = evevtModel;
  91. }else {
  92. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.Id source:AdvFourListGoodsAction];
  93. detail.eventModel = evevtModel;
  94. }
  95. [[self currentViewController].navigationController pushViewController:detail animated:YES];
  96. if (self.isOhterPage) {
  97. [MobClick event:CategoryRecommend label:self.name];
  98. }else {
  99. [MobClick event:TodayClickGoods label:self.name];
  100. }
  101. }
  102. - (UIViewController *)currentViewController{
  103. UIViewController * currVC = nil;
  104. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  105. UIViewController * Rootvc = window.rootViewController;
  106. do {
  107. if ([Rootvc isKindOfClass:[UINavigationController class]]) {
  108. UINavigationController * nav = (UINavigationController *)Rootvc;
  109. UIViewController * v = [nav.viewControllers lastObject];
  110. currVC = v;
  111. Rootvc = v.presentedViewController;
  112. continue;
  113. }else if([Rootvc isKindOfClass:[UITabBarController class]]){
  114. UITabBarController * tabVC = (UITabBarController *)Rootvc;
  115. currVC = tabVC;
  116. Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
  117. continue;
  118. }
  119. } while (Rootvc!=nil);
  120. return currVC;
  121. }
  122. @end