Files
real-e-party-iOS/YuMi/E-P/NewMoments/Views/EPMomentListView.m
edwinQQQ e8d59495a4 refactor: 重构 EPMomentViewController,替换 UITableView 为 EPMomentListView
主要变更:
1. 移除 UITableView,改为使用 EPMomentListView 以简化数据展示和交互。
2. 添加顶部固定文案 UILabel,提升用户体验。
3. 通过 EPMomentAPIHelper 统一管理 Moments 列表 API 请求,优化数据加载逻辑。
4. 更新 UI 约束,确保布局适配不同屏幕。

此重构旨在提升代码可维护性和用户界面的一致性。
2025-10-10 17:22:39 +08:00

144 lines
4.8 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// EPMomentListView.m
// YuMi
//
// Created by AI on 2025-10-10.
//
#import <UIKit/UIKit.h>
#import "EPMomentListView.h"
#import "EPMomentCell.h"
@interface EPMomentListView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@property (nonatomic, strong) NSMutableArray *mutableRawList;
@property (nonatomic, strong) EPMomentAPIHelper *api;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) BOOL isLoading;
@end
@implementation EPMomentListView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
_api = [[EPMomentAPIHelper alloc] init];
_mutableRawList = [NSMutableArray array];
_currentPage = 0; // 若后端从1开始这里会在首次请求时自增到1
_sourceType = EPMomentListSourceTypeRecommend;
[self addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
return self;
}
- (NSArray<NSMutableDictionary *> *)rawList {
return [self.mutableRawList copy];
}
- (void)reloadFirstPage {
self.currentPage = 0;
[self.mutableRawList removeAllObjects];
[self.tableView reloadData];
[self requestNextPage];
}
- (void)requestNextPage {
if (self.isLoading) return;
self.isLoading = YES;
NSLog(@"[EPMomentListView] 请求页码: %ld", (long)self.currentPage);
@kWeakify(self);
[self.api fetchMomentsWithType:self.sourceType page:self.currentPage pageSize:20 completion:^(NSArray <MomentsInfoModel *>* _Nullable list, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
self.isLoading = NO;
[self.refreshControl endRefreshing];
NSLog(@"[EPMomentListView] 返回 code=%ld, count=%lu", (long)code, (unsigned long)list.count);
if (code == 200 && list.count > 0) {
[self.mutableRawList addObjectsFromArray:list];
self.currentPage++;
[self.tableView reloadData];
} else if (code == 200 && list.count == 0 && self.currentPage == 0) {
// 如果第一页就为空尝试从1开始兼容某些后端从1计数
self.currentPage = 1;
[self requestNextPage];
}
}];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.mutableRawList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EPMomentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewMomentCell" forIndexPath:indexPath];
if (indexPath.row < self.mutableRawList.count) {
MomentsInfoModel *model = [self.mutableRawList xpSafeObjectAtIndex:indexPath.row];
[cell configureWithModel:model];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.onSelectMoment) self.onSelectMoment(indexPath.row);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height;
CGFloat screenHeight = scrollView.frame.size.height;
if (offsetY > contentHeight - screenHeight - 100 && !self.isLoading) {
[self requestNextPage];
}
}
#pragma mark - Lazy
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.estimatedRowHeight = 200;
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
[_tableView registerClass:[EPMomentCell class] forCellReuseIdentifier:@"NewMomentCell"];
_tableView.refreshControl = self.refreshControl;
}
return _tableView;
}
- (UIRefreshControl *)refreshControl {
if (!_refreshControl) {
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(reloadFirstPage) forControlEvents:UIControlEventValueChanged];
}
return _refreshControl;
}
@end