// // YMRankingListView.m // MSYOUPAI // // Created by YoMi on 2024/2/14. // Copyright © 2024 MS. All rights reserved. // #import "YMRankingListView.h" #import "YMRankTopView.h" #import "YMRankingListCell.h" @interface YMRankingListView() /// 排行榜VM @property (nonatomic, strong) YMRankingListSubCategoryViewModel *viewModel; /// 下拉刷新头部 @property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader; /// 上拉加载更多底部,还有更多数据可以加载的状态下使用 @property (nonatomic, strong) MJRefreshAutoNormalFooter *loadMoreFooter; /// 不会上拉加载更多的底部,没有更多数据可以加载的状态下使用 @property (nonatomic, strong) MJRefreshAutoNormalFooter *noLoadMoreFooter; /// 容器视图 @property (nonatomic, strong) UIView *contentView; /// 排名前三视图 @property (nonatomic, strong) YMRankTopView *topThreeView; /// 内容列表 @property (nonatomic, strong) UITableView *contentTableView; @end @implementation YMRankingListView - (void)dealloc{ self.scrollCallback = nil; } - (void)ym_setupViews{ [self addSubview:self.contentScrollView]; [self.contentScrollView addSubview:self.contentView]; [self.contentView addSubview:self.topThreeView]; [self.contentView addSubview:self.contentTableView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self); make.left.equalTo(self); make.right.equalTo(self); make.bottom.equalTo(self); }]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentScrollView); make.width.equalTo(self.contentScrollView.mas_width); }]; [self.topThreeView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView); make.left.equalTo(self.contentView); make.right.equalTo(self.contentView); make.height.mas_equalTo(adapt(230)); }]; [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.topThreeView.mas_bottom); make.left.equalTo(self.contentView).offset(0); make.right.equalTo(self.contentView).offset(-0); make.bottom.equalTo(self.contentView); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMRankingListSubCategoryViewModel*)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; [self.topThreeView ym_bindViewModel:self.viewModel]; [self headerRefreshing]; @weakify(self) [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.contentTableView reloadData]; [self.contentTableView layoutIfNeeded]; //刷新高度 CGFloat tableViewHeight = self.contentTableView.contentSize.height < kFrameHeight - kYMNavHeight - adapt(50) ? kFrameHeight - kYMNavHeight - adapt(50) : self.contentTableView.contentSize.height; [self.contentTableView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(tableViewHeight); }]; switch ([result intValue]) { case YMHeaderRefresh_HasMoreData: case YMHeaderRefresh_HasNoMoreData: { [self.contentScrollView.mj_header endRefreshing]; [self.contentScrollView.mj_footer endRefreshing]; } break; case YMFooterRefresh_HasMoreData: { [self.contentScrollView.mj_header endRefreshing]; [self.contentScrollView.mj_footer endRefreshing]; if (self.contentScrollView.mj_footer == self.noLoadMoreFooter) { self.contentScrollView.mj_footer = self.loadMoreFooter; } } break; case YMFooterRefresh_HasNoMoreData: { [self.contentScrollView.mj_header endRefreshing]; [self.contentScrollView.mj_footer endRefreshing]; if (self.contentScrollView.mj_footer == self.loadMoreFooter) { self.contentScrollView.mj_footer = self.noLoadMoreFooter; } } break; case YMRefreshError: { [self.contentScrollView.mj_header endRefreshing]; [self.contentScrollView.mj_footer endRefreshing]; } break; default: break; } [self.contentTableView ym_endLoading]; }]; } - (void)headerRefreshing{ [self.contentTableView ym_startLoading]; self.viewModel.currentPage = 1; [self.viewModel getRankingListData]; } - (void)footerRefreshing{ [self.contentTableView ym_startLoading]; self.viewModel.currentPage++; [self.viewModel getRankingListData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.viewModel.listDataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ YMRankingListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YMRankingListCell class])]; if (!cell) { cell = [[YMRankingListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([YMRankingListCell class])]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell ym_bindViewModel:self.viewModel.listDataArray[indexPath.item]]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return adapt(62); } //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { // return 235; //} // //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // return self.topThreeView; //} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (self.viewModel.listDataArray[indexPath.item].rankingUserId != 0) { [self.viewModel.gotoPersonalPageSubject sendNext:@(self.viewModel.listDataArray[indexPath.item].rankingUserId)]; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { !self.scrollCallback ?: self.scrollCallback(scrollView); } #pragma mark - JXCategoryListContentViewDelegate - (UIView *)listView { return self; } - (MJRefreshNormalHeader *)refreshHeader{ if(!_refreshHeader){ // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的headerRefreshing方法) _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)]; _refreshHeader.backgroundColor = UIColor.clearColor; _refreshHeader.lastUpdatedTimeLabel.textColor = DecColorFromRGBA(0, 0, 0, 0.5); _refreshHeader.stateLabel.textColor = DecColorFromRGBA(0, 0, 0, 0.5); } return _refreshHeader; } - (MJRefreshAutoNormalFooter *)loadMoreFooter{ if(!_loadMoreFooter){ // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的footerRefreshing方法) _loadMoreFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)]; _loadMoreFooter.triggerAutomaticallyRefreshPercent = 0.1; _loadMoreFooter.backgroundColor = UIColor.clearColor; _loadMoreFooter.stateLabel.textColor = DecColorFromRGBA(0, 0, 0, 0.5); } return _loadMoreFooter; } - (MJRefreshAutoNormalFooter *)noLoadMoreFooter{ if(!_noLoadMoreFooter){ _noLoadMoreFooter = [[MJRefreshAutoNormalFooter alloc]initWithFrame:CGRectZero]; _noLoadMoreFooter.backgroundColor = [UIColor clearColor]; _noLoadMoreFooter.state = MJRefreshStateIdle; _noLoadMoreFooter.mj_h = 0;//重新设置高度,不让看到它,并且当设置为table.footer时,table会自动调整自己的高度 _noLoadMoreFooter.clipsToBounds = YES;//不让看不到菊花 } return _noLoadMoreFooter; } - (UIScrollView *)contentScrollView{ if (!_contentScrollView) { _contentScrollView = [[UIScrollView alloc] init]; _contentScrollView.scrollEnabled = YES; _contentScrollView.alwaysBounceVertical = YES; _contentScrollView.showsVerticalScrollIndicator = NO; _contentScrollView.showsHorizontalScrollIndicator = NO; _contentScrollView.backgroundColor = UIColor.clearColor; _contentScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; _contentScrollView.mj_header = self.refreshHeader; _contentScrollView.mj_footer = self.noLoadMoreFooter; } return _contentScrollView; } - (UIView *)contentView{ if (!_contentView) { _contentView = [[UIView alloc]init]; } return _contentView; } - (YMRankTopView *)topThreeView{ if (!_topThreeView) { _topThreeView = [[YMRankTopView alloc]init]; _topThreeView.backgroundColor = UIColor.clearColor; } return _topThreeView; } - (UITableView *)contentTableView{ if (!_contentTableView) { _contentTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _contentTableView.delegate = self; _contentTableView.dataSource = self; _contentTableView.estimatedRowHeight = 0; _contentTableView.estimatedSectionHeaderHeight = 0; _contentTableView.estimatedSectionFooterHeight = 0; _contentTableView.showsVerticalScrollIndicator = NO; _contentTableView.showsHorizontalScrollIndicator = NO; _contentTableView.scrollEnabled = NO; _contentTableView.separatorColor = UIColor.clearColor; _contentTableView.backgroundColor = UIColor.whiteColor; [_contentTableView registerClass:[YMRankingListCell class] forCellReuseIdentifier:NSStringFromClass([YMRankingListCell class])]; YMEmptyView *empty = [YMEmptyView emptyViewWithImageStr:@"ym_common_no_data_icon" titleStr:@"" detailStr:@""]; empty.contentViewY = adapt(100); empty.imageSize = kEmptyViewSize; _contentTableView.ym_emptyView = empty; _contentTableView.ym_emptyView.autoShowEmptyView = NO; _contentTableView.layer.cornerRadius = 20; _contentTableView.layer.masksToBounds = YES; //_contentTableView.layer.borderColor = UIColor.whiteColor.CGColor; //_contentTableView.layer.borderWidth = 1; } return _contentTableView; } @end