Files
yinmeng-ios/xplan-ios/Main/Monents/View/SubViews/XPMonentsRecommendHeaderView.m
2022-11-11 17:46:37 +08:00

146 lines
5.3 KiB
Objective-C

//
// XPMonentsRecommendHeaderView.m
// xplan-ios
//
// Created by 冯硕 on 2022/5/18.
//
#import "XPMonentsRecommendHeaderView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "NSArray+Safe.h"
///Model
#import "MonentsTopicModel.h"
///View
#import "XPMonentsTopicCollectionViewCell.h"
@interface XPMonentsRecommendHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///显示标题
@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(12);
make.height.mas_equalTo(26);
}];
}
#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 {
MonentsTopicModel * topicModel = [self.topicList safeObjectAtIndex1:indexPath.row];
CGFloat itemWidth = [topicModel.name boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightBold]} context:nil].size.width;
return CGSizeMake(itemWidth +34, 26);
}
- (__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<MonentsTopicModel *> *)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