// // XPHomeSearchRelateView.m // xplan-ios // // Created by GreenLand on 2022/11/16. // #import "XPHomeSearchRelateView.h" #import "Masonry/Masonry.h" #import "NSArray+Safe.h" #import "ThemeColor.h" #import "XPMacro.h" ///view #import "XPSearchListTableViewCell.h" #import "XPHomeRedommendCollectionViewCell.h" #import "XPRoomSearchRecommendHeadView.h" ///Model #import "HomeSearchResultModel.h" #import "HomeRecommendRoomModel.h" @interface XPHomeSearchRelateView() @property (nonatomic, strong) UILabel *relateTitle; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation XPHomeSearchRelateView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initView]; [self initContraints]; } return self; } - (void)initView { self.backgroundColor = [ThemeColor appBackgroundColor]; [self addSubview:self.relateTitle]; [self addSubview:self.tableView]; [self addSubview:self.collectionView]; } - (void)initContraints { [self.relateTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(16); make.left.mas_equalTo(15); make.height.mas_equalTo(20); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(0); make.top.mas_equalTo(self.relateTitle.mas_bottom).mas_offset(16); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0); }]; } #pragma mark - tableviewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.relateArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { XPSearchListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPSearchListTableViewCell class])]; if (cell == nil) { cell = [[XPSearchListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPSearchListTableViewCell class])]; } HomeRecommendRoomModel * resultModel = [self.relateArray safeObjectAtIndex1:indexPath.row]; [cell configEveryoneData:resultModel]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { HomeRecommendRoomModel * resultModel = [self.relateArray safeObjectAtIndex1:indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchRelateViewEnterRoom:)]) { [self.delegate xPHomeSearchRelateViewEnterRoom:resultModel.uid]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 74; } #pragma mark - collectionViewDelegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.recommendArray.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPHomeRedommendCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeRedommendCollectionViewCell class]) forIndexPath:indexPath]; HomeRecommendRoomModel * info = [self.recommendArray safeObjectAtIndex1:indexPath.row]; cell.recommendRoomInfo = info; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100 * kScreenScale, 100 * kScreenScale); } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { XPRoomSearchRecommendHeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([XPRoomSearchRecommendHeadView class]) forIndexPath:indexPath]; return headView; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.recommendArray.count > 0) { HomeRecommendRoomModel * recommend = [self.recommendArray safeObjectAtIndex1:indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeSearchRelateViewEnterRoom:)]) { [self.delegate xPHomeSearchRelateViewEnterRoom:recommend.roomUid]; } } } - (void)setRelateArray:(NSArray *)relateArray { _relateArray = relateArray; [self.tableView reloadData]; self.collectionView.hidden = YES; self.relateTitle.hidden = NO; self.tableView.hidden = NO; } - (void)setRecommendArray:(NSArray *)recommendArray { _recommendArray = recommendArray; [self.collectionView reloadData]; self.collectionView.hidden = NO; self.relateTitle.hidden = YES; self.tableView.hidden = YES; } - (UILabel *)relateTitle { if (!_relateTitle) { UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; label.textColor = [ThemeColor mainTextColor]; label.text = @"相关房间"; _relateTitle = label; } return _relateTitle; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableHeaderView = [UIView new]; _tableView.tableFooterView = [UIView new]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor clearColor]; _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; [_tableView registerClass:[XPSearchListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPSearchListTableViewCell class])]; } return _tableView; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.headerReferenceSize = CGSizeMake(KScreenWidth, 200); layout.minimumLineSpacing = 8; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.sectionInset = UIEdgeInsetsMake(15, 15, 0, 15); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[XPHomeRedommendCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeRedommendCollectionViewCell class])]; [_collectionView registerClass:[XPRoomSearchRecommendHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([XPRoomSearchRecommendHeadView class])]; } return _collectionView; } @end