| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // JXHotRecommendView.m
- // AICity
- //
- // 热门推荐组件实现
- //
- #import "JXHotRecommendView.h"
- #import <SDWebImage/SDWebImage.h>
- #import <Masonry/Masonry.h>
- static NSString * const kHotRecommendCellIdentifier = @"HotRecommendCell";
- static const CGFloat kCellWidth = 120.0;
- static const CGFloat kCellHeight = 180.0;
- @interface JXHotRecommendCell : UICollectionViewCell
- @property (nonatomic, strong) UIImageView *coverImageView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UILabel *descLabel;
- @end
- @implementation JXHotRecommendCell
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- // 封面图
- self.coverImageView = [[UIImageView alloc] init];
- self.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
- self.coverImageView.clipsToBounds = YES;
- self.coverImageView.layer.cornerRadius = 8;
- [self.contentView addSubview:self.coverImageView];
-
- // 标题
- self.titleLabel = [[UILabel alloc] init];
- self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
- self.titleLabel.textColor = [UIColor blackColor];
- self.titleLabel.numberOfLines = 2;
- [self.contentView addSubview:self.titleLabel];
-
- // 描述(集数)
- self.descLabel = [[UILabel alloc] init];
- self.descLabel.font = [UIFont systemFontOfSize:12];
- self.descLabel.textColor = [UIColor grayColor];
- [self.contentView addSubview:self.descLabel];
-
- // Layout
- [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.equalTo(self.contentView);
- make.height.mas_equalTo(kCellHeight - 60);
- }];
-
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.coverImageView.mas_bottom).offset(4);
- make.left.right.equalTo(self.contentView);
- }];
-
- [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
- make.left.right.equalTo(self.contentView);
- }];
- }
- return self;
- }
- @end
- #pragma mark - Hot Recommend View
- @interface JXHotRecommendView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @end
- @implementation JXHotRecommendView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
- self.backgroundColor = [UIColor whiteColor];
-
- // 标题
- self.titleLabel = [[UILabel alloc] init];
- self.titleLabel.text = @"热门推荐";
- self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
- self.titleLabel.textColor = [UIColor blackColor];
- [self addSubview:self.titleLabel];
-
- // CollectionView Layout
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- layout.minimumLineSpacing = 12;
- layout.minimumInteritemSpacing = 0;
- layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
-
- // CollectionView
- self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- self.collectionView.backgroundColor = [UIColor whiteColor];
- self.collectionView.showsHorizontalScrollIndicator = NO;
- [self.collectionView registerClass:[JXHotRecommendCell class] forCellWithReuseIdentifier:kHotRecommendCellIdentifier];
- [self addSubview:self.collectionView];
-
- // Layout
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(12);
- make.left.equalTo(self).offset(16);
- make.right.equalTo(self).offset(-16);
- make.height.mas_equalTo(25);
- }];
-
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
- make.left.right.bottom.equalTo(self);
- }];
- }
- - (void)updateItems:(NSArray<JXBannerItem *> *)items {
- self.items = items;
- [self.collectionView reloadData];
- }
- #pragma mark - UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.items.count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- JXHotRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHotRecommendCellIdentifier forIndexPath:indexPath];
-
- JXBannerItem *item = self.items[indexPath.item];
-
- // 优先使用竖向封面
- NSString *imageUrl = item.vCoverUrl ?: item.hCoverUrl;
- if (imageUrl) {
- [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
- placeholderImage:nil];
- }
-
- cell.titleLabel.text = item.name;
- cell.descLabel.text = item.describe;
-
- return cell;
- }
- #pragma mark - UICollectionViewDelegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- JXBannerItem *item = self.items[indexPath.item];
- if (self.onItemClick) {
- self.onItemClick(item);
- }
- }
- #pragma mark - UICollectionViewDelegateFlowLayout
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(kCellWidth, kCellHeight);
- }
- @end
|