No Description

KXRecommendView.m 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // KXRecommendView.m
  3. // CAISHEN
  4. //
  5. // Created by kuxuan on 2017/8/28.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXRecommendView.h"
  9. #import "KXRecommendCollectionViewCell.h"
  10. #import "KXRecommnedModel.h"
  11. @implementation KXRecommendView
  12. {
  13. UICollectionView *_collectionView;
  14. }
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. self.backgroundColor = [UIColor whiteColor];
  19. [self setupUI];
  20. }
  21. return self;
  22. }
  23. - (void)setupUI
  24. {
  25. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  26. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  27. flowLayout.itemSize = CGSizeMake(78*SCREEN_MUTI, 90*SCREEN_MUTI);
  28. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
  29. _collectionView.backgroundColor = [UIColor whiteColor];
  30. _collectionView.showsHorizontalScrollIndicator = NO;
  31. [_collectionView registerClass:[KXRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  32. _collectionView.dataSource = self;
  33. _collectionView.delegate = self;
  34. [self addSubview:_collectionView];
  35. }
  36. - (void)setDataSource:(NSArray *)dataSource
  37. {
  38. _dataSource = dataSource;
  39. [_collectionView reloadData];
  40. }
  41. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  42. {
  43. return self.dataSource.count;
  44. }
  45. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  46. {
  47. KXRecommendCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  48. KXRecommnedModel *model = self.dataSource[indexPath.row];
  49. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"placeholder_square"] options:0];
  50. cell.titleLabel.text = model.name;
  51. return cell;
  52. }
  53. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  54. {
  55. KXRecommnedModel *model = self.dataSource[indexPath.item];
  56. [_delegate recmmendViewClickWithId:model.Id name:model.name];
  57. }
  58. @end