// // XPMonentsRecommendHeaderView.m // xplan-ios // // Created by 冯硕 on 2022/5/18. // #import "XPMonentsRecommendHeaderView.h" ///Third #import ///Tool #import "ThemeColor.h" #import "NSArray+Safe.h" ///Model #import "MonentsTopicModel.h" ///View #import "XPMonentsTopicCollectionViewCell.h" @interface XPMonentsRecommendHeaderView () ///显示标题 @property (nonatomic,strong) UILabel *titleLabel; ///显示箭头 @property (nonatomic,strong) UIButton *arrowButton; ///列表 @property (nonatomic,strong) UICollectionView *collectionView; @end @implementation XPMonentsRecommendHeaderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { self.backgroundColor = [ThemeColor appCellBackgroundColor]; [self addSubview:self.titleLabel]; [self addSubview:self.arrowButton]; [self addSubview:self.collectionView]; } - (void)initSubViewConstraints { [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self).offset(15); make.top.mas_equalTo(self).offset(15); }]; [self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(11, 20)); make.centerY.mas_equalTo(self.titleLabel); make.right.mas_equalTo(self).offset(-15); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self); make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10); make.height.mas_equalTo(77); }]; } #pragma mark - UICollectionViewDelegate And UICollectionViewDatasource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.topicList.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(125, 77); } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPMonentsTopicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMonentsTopicCollectionViewCell class]) forIndexPath:indexPath]; cell.topicInfo = [self.topicList safeObjectAtIndex1:indexPath.row]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.topicList.count > 0) { MonentsTopicModel * topicInfo = [self.topicList safeObjectAtIndex1:indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsRecommendHeaderView:didSelectItem:)]) { [self.delegate xPMonentsRecommendHeaderView:self didSelectItem:topicInfo]; } } } #pragma mark - Event Response - (void)arrowButtonAction:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsRecommendHeaderView:didClickMoreTopic:)]) { [self.delegate xPMonentsRecommendHeaderView:self didClickMoreTopic:sender]; } } #pragma mark - Getters And Setters - (void)setTopicList:(NSArray *)topicList { _topicList = topicList; [self.collectionView reloadData]; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _titleLabel.textColor = [ThemeColor mainTextColor]; _titleLabel.text = @"推荐话题"; } return _titleLabel; } - (UIButton *)arrowButton { if (!_arrowButton) { _arrowButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_arrowButton setImage:[UIImage imageNamed:@"room_setting_arrow"] forState:UIControlStateNormal]; [_arrowButton setImage:[UIImage imageNamed:@"room_setting_arrow"] forState:UIControlStateSelected]; [_arrowButton addTarget:self action:@selector(arrowButtonAction:) forControlEvents:UIControlEventTouchUpInside]; } return _arrowButton; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 0); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = 8; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.showsHorizontalScrollIndicator = NO; [_collectionView registerClass:[XPMonentsTopicCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMonentsTopicCollectionViewCell class])]; } return _collectionView; } @end