JXHotRecommendView.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // JXHotRecommendView.m
  3. // AICity
  4. //
  5. // 热门推荐组件实现
  6. //
  7. #import "JXHotRecommendView.h"
  8. #import <SDWebImage/SDWebImage.h>
  9. #import <Masonry/Masonry.h>
  10. static NSString * const kHotRecommendCellIdentifier = @"HotRecommendCell";
  11. static const CGFloat kCellWidth = 120.0;
  12. static const CGFloat kCellHeight = 180.0;
  13. @interface JXHotRecommendCell : UICollectionViewCell
  14. @property (nonatomic, strong) UIImageView *coverImageView;
  15. @property (nonatomic, strong) UILabel *titleLabel;
  16. @property (nonatomic, strong) UILabel *descLabel;
  17. @end
  18. @implementation JXHotRecommendCell
  19. - (instancetype)initWithFrame:(CGRect)frame {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. // 封面图
  23. self.coverImageView = [[UIImageView alloc] init];
  24. self.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
  25. self.coverImageView.clipsToBounds = YES;
  26. self.coverImageView.layer.cornerRadius = 8;
  27. [self.contentView addSubview:self.coverImageView];
  28. // 标题
  29. self.titleLabel = [[UILabel alloc] init];
  30. self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
  31. self.titleLabel.textColor = [UIColor blackColor];
  32. self.titleLabel.numberOfLines = 2;
  33. [self.contentView addSubview:self.titleLabel];
  34. // 描述(集数)
  35. self.descLabel = [[UILabel alloc] init];
  36. self.descLabel.font = [UIFont systemFontOfSize:12];
  37. self.descLabel.textColor = [UIColor grayColor];
  38. [self.contentView addSubview:self.descLabel];
  39. // Layout
  40. [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.top.left.right.equalTo(self.contentView);
  42. make.height.mas_equalTo(kCellHeight - 60);
  43. }];
  44. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.equalTo(self.coverImageView.mas_bottom).offset(4);
  46. make.left.right.equalTo(self.contentView);
  47. }];
  48. [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
  50. make.left.right.equalTo(self.contentView);
  51. }];
  52. }
  53. return self;
  54. }
  55. @end
  56. #pragma mark - Hot Recommend View
  57. @interface JXHotRecommendView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  58. @property (nonatomic, strong) UILabel *titleLabel;
  59. @property (nonatomic, strong) UICollectionView *collectionView;
  60. @end
  61. @implementation JXHotRecommendView
  62. - (instancetype)initWithFrame:(CGRect)frame {
  63. self = [super initWithFrame:frame];
  64. if (self) {
  65. [self setupUI];
  66. }
  67. return self;
  68. }
  69. - (void)setupUI {
  70. self.backgroundColor = [UIColor whiteColor];
  71. // 标题
  72. self.titleLabel = [[UILabel alloc] init];
  73. self.titleLabel.text = @"热门推荐";
  74. self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  75. self.titleLabel.textColor = [UIColor blackColor];
  76. [self addSubview:self.titleLabel];
  77. // CollectionView Layout
  78. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  79. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  80. layout.minimumLineSpacing = 12;
  81. layout.minimumInteritemSpacing = 0;
  82. layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
  83. // CollectionView
  84. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  85. self.collectionView.delegate = self;
  86. self.collectionView.dataSource = self;
  87. self.collectionView.backgroundColor = [UIColor whiteColor];
  88. self.collectionView.showsHorizontalScrollIndicator = NO;
  89. [self.collectionView registerClass:[JXHotRecommendCell class] forCellWithReuseIdentifier:kHotRecommendCellIdentifier];
  90. [self addSubview:self.collectionView];
  91. // Layout
  92. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.top.equalTo(self).offset(12);
  94. make.left.equalTo(self).offset(16);
  95. make.right.equalTo(self).offset(-16);
  96. make.height.mas_equalTo(25);
  97. }];
  98. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
  100. make.left.right.bottom.equalTo(self);
  101. }];
  102. }
  103. - (void)updateItems:(NSArray<JXBannerItem *> *)items {
  104. self.items = items;
  105. [self.collectionView reloadData];
  106. }
  107. #pragma mark - UICollectionViewDataSource
  108. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  109. return self.items.count;
  110. }
  111. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  112. JXHotRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHotRecommendCellIdentifier forIndexPath:indexPath];
  113. JXBannerItem *item = self.items[indexPath.item];
  114. // 优先使用竖向封面
  115. NSString *imageUrl = item.vCoverUrl ?: item.hCoverUrl;
  116. if (imageUrl) {
  117. [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
  118. placeholderImage:nil];
  119. }
  120. cell.titleLabel.text = item.name;
  121. cell.descLabel.text = item.describe;
  122. return cell;
  123. }
  124. #pragma mark - UICollectionViewDelegate
  125. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  126. JXBannerItem *item = self.items[indexPath.item];
  127. if (self.onItemClick) {
  128. self.onItemClick(item);
  129. }
  130. }
  131. #pragma mark - UICollectionViewDelegateFlowLayout
  132. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  133. return CGSizeMake(kCellWidth, kCellHeight);
  134. }
  135. @end