refactor: 重构 EPMomentViewController,替换 UITableView 为 EPMomentListView

主要变更:
1. 移除 UITableView,改为使用 EPMomentListView 以简化数据展示和交互。
2. 添加顶部固定文案 UILabel,提升用户体验。
3. 通过 EPMomentAPIHelper 统一管理 Moments 列表 API 请求,优化数据加载逻辑。
4. 更新 UI 约束,确保布局适配不同屏幕。

此重构旨在提升代码可维护性和用户界面的一致性。
This commit is contained in:
edwinQQQ
2025-10-10 17:22:39 +08:00
parent 8b177e5fad
commit e8d59495a4
9 changed files with 396 additions and 588 deletions

View File

@@ -7,34 +7,20 @@
//
#import "EPMomentViewController.h"
#import <UIKit/UIKit.h>
#import <Masonry/Masonry.h>
#import "EPMomentCell.h"
#import "Api+Moments.h"
#import "AccountInfoStorage.h"
#import "MomentsInfoModel.h"
#import "EPMomentListView.h"
@interface EPMomentViewController () <UITableViewDelegate, UITableViewDataSource>
@interface EPMomentViewController ()
// MARK: - UI Components
///
@property (nonatomic, strong) UITableView *tableView;
/// MVVMView
@property (nonatomic, strong) EPMomentListView *listView;
///
@property (nonatomic, strong) UIRefreshControl *refreshControl;
///
@property (nonatomic, strong) UIButton *publishButton;
// MARK: - Data
/// MomentsInfoModel
@property (nonatomic, strong) NSMutableArray<MomentsInfoModel *> *dataSource;
///
@property (nonatomic, assign) NSInteger currentPage;
///
@property (nonatomic, assign) BOOL isLoading;
///
@property (nonatomic, strong) UILabel *topTipLabel;
@end
@@ -48,7 +34,7 @@
self.title = @"动态";
[self setupUI];
[self loadData];
[self.listView reloadFirstPage];
NSLog(@"[EPMomentViewController] 页面加载完成");
}
@@ -74,70 +60,29 @@
make.edges.mas_equalTo(self.view);
}];
// TableView
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
//
[self.view addSubview:self.topTipLabel];
[self.topTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(8);
make.left.equalTo(self.view).offset(20);
make.right.equalTo(self.view).offset(-20);
}];
// TODO:
[self.view addSubview:self.publishButton];
[self.publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-20);
make.bottom.equalTo(self.view).offset(-100); // TabBar
make.size.mas_equalTo(CGSizeMake(56, 56));
//
[self.view addSubview:self.listView];
[self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view);
make.top.equalTo(self.topTipLabel.mas_bottom).offset(8);
}];
//
UIBarButtonItem *publishItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onPublishButtonTapped)];
self.navigationItem.rightBarButtonItem = publishItem;
NSLog(@"[EPMomentViewController] UI 设置完成");
}
// MARK: - Data Loading
- (void)loadData {
if (self.isLoading) return;
self.isLoading = YES;
NSLog(@"[EPMomentViewController] 开始加载数据,页码: %ld", (long)self.currentPage);
// API
NSString *page = [NSString stringWithFormat:@"%ld", (long)self.currentPage];
NSString *pageSize = @"10";
NSString *types = @"0,2"; // 0=2=
@kWeakify(self);
[Api momentsRecommendList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
@kStrongify(self);
self.isLoading = NO;
[self.refreshControl endRefreshing];
if (code == 200 && data.data) {
//
NSArray *list = [MomentsInfoModel mj_objectArrayWithKeyValuesArray:data.data];
if (list.count > 0) {
[self.dataSource addObjectsFromArray:list];
self.currentPage++;
[self.tableView reloadData];
NSLog(@"[EPMomentViewController] 加载成功,新增 %lu 条动态", (unsigned long)list.count);
} else {
NSLog(@"[EPMomentViewController] 没有更多数据");
}
} else {
NSLog(@"[EPMomentViewController] 加载失败: code=%ld, msg=%@", (long)code, msg);
// API
if (self.dataSource.count == 0) {
//
[self showAlertWithMessage:msg ?: @"加载失败"];
}
}
} page:page pageSize:pageSize types:types];
}
- (void)onRefresh {
self.currentPage = 0;
[self.dataSource removeAllObjects];
[self loadData];
}
// VC /
// MARK: - Actions
@@ -155,108 +100,35 @@
[self presentViewController:alert animated:YES completion:nil];
}
// MARK: - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EPMomentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewMomentCell" forIndexPath:indexPath];
if (indexPath.row < self.dataSource.count) {
MomentsInfoModel *model = self.dataSource[indexPath.row];
[cell configureWithModel:model];
}
return cell;
}
// MARK: - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"[EPMomentViewController] 点击动态: %ld", (long)indexPath.row);
// TODO:
[self showAlertWithMessage:[NSString stringWithFormat:@"点击了第 %ld 条动态", (long)indexPath.row]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 200;
}
//
- (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 loadData];
}
}
// listView
// MARK: - Lazy Loading
- (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);
// Cell
[_tableView registerClass:[EPMomentCell class] forCellReuseIdentifier:@"NewMomentCell"];
//
_tableView.refreshControl = self.refreshControl;
// Lazy
- (EPMomentListView *)listView {
if (!_listView) {
_listView = [[EPMomentListView alloc] initWithFrame:CGRectZero];
__weak typeof(self) weakSelf = self;
_listView.onSelectMoment = ^(NSInteger index) {
__strong typeof(weakSelf) self = weakSelf;
[self showAlertWithMessage:[NSString stringWithFormat:@"点击了第 %ld 条动态", (long)index]];
};
}
return _tableView;
return _listView;
}
- (UIRefreshControl *)refreshControl {
if (!_refreshControl) {
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(onRefresh) forControlEvents:UIControlEventValueChanged];
- (UILabel *)topTipLabel {
if (!_topTipLabel) {
_topTipLabel = [UILabel new];
_topTipLabel.numberOfLines = 0;
_topTipLabel.textColor = [UIColor whiteColor];
_topTipLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
_topTipLabel.text = @"The disease is like a cruel ruler, measuring the true length of my life, but it is also like a lamp, illuminating the present that I have always ignored. Now I feel a strange freedom: since the end is clear, I can take every step with my whole heart.";
}
return _refreshControl;
return _topTipLabel;
}
- (UIButton *)publishButton {
if (!_publishButton) {
_publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
_publishButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.6 blue:0.86 alpha:1.0]; //
_publishButton.layer.cornerRadius = 28;
_publishButton.layer.shadowColor = [UIColor blackColor].CGColor;
_publishButton.layer.shadowOffset = CGSizeMake(0, 2);
_publishButton.layer.shadowOpacity = 0.3;
_publishButton.layer.shadowRadius = 4;
// 使
[_publishButton setTitle:@"+" forState:UIControlStateNormal];
_publishButton.titleLabel.font = [UIFont systemFontOfSize:32 weight:UIFontWeightLight];
[_publishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_publishButton addTarget:self action:@selector(onPublishButtonTapped) forControlEvents:UIControlEventTouchUpInside];
}
return _publishButton;
}
- (NSMutableArray *)dataSource {
if (!_dataSource) {
_dataSource = [NSMutableArray array];
}
return _dataSource;
}
//
@end