Files
yinmeng-ios/xplan-ios/Main/Home/View/Cell/XPHomeRecommendTableViewCell.m
2022-11-11 17:46:37 +08:00

112 lines
4.2 KiB
Objective-C

//
// XPHomeRecommendTableViewCell.m
// xplan-ios
//
// Created by 冯硕 on 2022/2/21.
// 最新推荐
#import "XPHomeRecommendTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMacro.h"
#import "NSArray+Safe.h"
///View
#import "XPHomeRedommendCollectionViewCell.h"
@interface XPHomeRecommendTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation XPHomeRecommendTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.collectionView];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15);
make.top.bottom.mas_equalTo(self.contentView);
}];
}
#pragma mark - Event Response
- (void)tapRecognizer:(UITapGestureRecognizer *)tap {
NSInteger index = tap.view.tag - 1001;
if (index < self.recommendList.count) {
HomeRecommendRoomModel * recommend = [_recommendList safeObjectAtIndex1:index];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeRecommendTableViewCell:didClickItem:)]) {
[self.delegate xPHomeRecommendTableViewCell:self didClickItem:recommend];
}
}
}
#pragma mark - UICollectionViewDelegate And UICollectionViewDatasource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100 * kScreenScale, 100 * kScreenScale);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.recommendList.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPHomeRedommendCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeRedommendCollectionViewCell class]) forIndexPath:indexPath];
HomeRecommendRoomModel * info = [self.recommendList safeObjectAtIndex1:indexPath.row];
cell.recommendRoomInfo = info;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.recommendList.count > 0) {
HomeRecommendRoomModel * recommend = [self.recommendList safeObjectAtIndex1:indexPath.row];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeRecommendTableViewCell:didClickItem:)]) {
[self.delegate xPHomeRecommendTableViewCell:self didClickItem:recommend];
}
}
}
#pragma mark - Getters And Setters
- (void)setRecommendList:(NSArray *)recommendList {
_recommendList = recommendList;
[self.collectionView reloadData];
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 8 * kScreenScale;
layout.minimumInteritemSpacing = 0;
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.tag = 1001;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.showsHorizontalScrollIndicator = NO;
[_collectionView registerClass:[XPHomeRedommendCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeRedommendCollectionViewCell class])];
}
return _collectionView;
}
@end