持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第18天,点击查看活动详情
引言
需求:由于消息列表,数据量比较大,为了提升用户体验,需采用分页加载显示数据
案例:iOS零售版ERP APP增加支付奖励消息通知
通知信息(定时xx点;历史消息可查)
2021-04-29 尊敬的商家,您参与的xxx激励金活动,昨日参与成功10笔,共获得激励金1元!
I 集成下/上拉刷新控件
1.1 定义相关分页属性
- 分页属性
@property (nonatomic , assign) NSInteger pageNum;//当前页码
@property (nonatomic , assign) NSInteger pageCount;// 总页数
@property (nonatomic , assign) BOOL isfooterRereshing;
// 每页显示数...
- VM中的事件和数据属性
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,strong) RACSubject *reloadSubject;
@property (nonatomic,strong) RACSubject *ShowNoviewSubject;
@property (nonatomic,strong) RACSubject *hidenNoviewSubject;
1.2 监听下拉和上拉事件
- VC 监听和处理下拉和上拉事件
_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
- 处理上拉加载数据事件
/** 用于标志下拉动作*/
@property (nonatomic , assign) BOOL isfooterRereshing;
- (void)footerRereshing
{
self.isfooterRereshing = YES;
if ((_pageNum + 1) > _pageCount) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
return;
}
_pageNum = _pageNum + 1;
[self doorRequest];
}
- 处理下拉刷新数据事件
- (void)headerRereshing
{
self.isfooterRereshing = NO;
[_doorArr removeAllObjects];// 移除数据,可请求成功之后,再移除
_pageNum = 1;
[self doorRequest];
}
1.3 请求数据的处理
请求成功和失败都要关闭刷新视图
[weakSelf.vcView.tableView.mj_footer endRefreshing];
[weakSelf.vcView.tableView.mj_header endRefreshing];
完成处理的代码
- (void)doorRequest
{
//暂无数据
if (self.viewModel.datas.count == 0) {
[self.viewModel.ShowNoviewSubject sendNext:QCTLocal(@"no_data")];
}else{
[self.viewModel.hidenNoviewSubject sendNext:QCTLocal(@"")];
}
if(![UserInfoModel.shareUserInfoModel ispayStoreId]){
// [self showHUDMessage:@"请先进件"];
// 显示暂无数据
return;
}
NSString *post = [NSString stringWithFormat:@"%@%@",currentPayHost,@""];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
//111850
// [params setValue:@"" forKey:@"sid"];
[params setValue:UserInfoModel.shareUserInfoModel.store.payStoreId forKey:@"sid"];
[params setValue:[[NSNumber numberWithInteger:self.pageNum]description] forKey:@"page"];
[params setValue:kPageSize forKey:@"pageSize"];
__weak __typeof__(self) weakSelf = self;
[QCTNetworkHelper Post:post parameters:params success:^(NSDictionary* responseObj) {
NSDictionary *data = nil;
if([responseObj.allKeys containsObject:@"data"]){
data = responseObj[@"data"];
}else{
[self showHUDMessage:@"数据异常!"];
// [SVProgressHUD showInfoWithStatus:@"数据异常!"];
return;// 获取数据失败
}
if([data.allKeys containsObject:@"data"]){
data = responseObj[@"data"];
}else{
// [SVProgressHUD showInfoWithStatus:@"数据异常!"];
[self showHUDMessage:@"数据异常!"];
return;// 获取数据失败
}
NSMutableArray* tmparrresult = [ERPTradeRewardReportDto mj_objectArrayWithKeyValuesArray:data[@"data"]];
if(self.isfooterRereshing){
[weakSelf.viewModel.datas addObjectsFromArray:tmparrresult];
}else{
weakSelf.viewModel.datas = tmparrresult ;
}
[weakSelf.vcView.tableView reloadData];
weakSelf.pageCount = [responseObj[@"data"][@"pageCount"] integerValue];
weakSelf.pageNum = [responseObj[@"data"][@"page"] integerValue];
[weakSelf.vcView.tableView.mj_footer endRefreshing];
[weakSelf.vcView.tableView.mj_header endRefreshing];
if (weakSelf.viewModel.datas.count == 0) {
[weakSelf.viewModel.ShowNoviewSubject sendNext:QCTLocal(@"no_data")];
}else{
[weakSelf.viewModel.hidenNoviewSubject sendNext:QCTLocal(@"no_data")];
}
} failure:^(NSError * _Nonnull error) {
[QCTNetworkHelper showLoading_failed_please_try_again_laterBlock];
[self.vcView.tableView.mj_footer endRefreshing];
[self.vcView.tableView.mj_header endRefreshing];
} bizFailure:^(id _Nonnull responseObj) {
[self.vcView.tableView.mj_footer endRefreshing];
[self.vcView.tableView.mj_header endRefreshing];
[QCTNetworkHelper showresponseObjmessage:responseObj];
} isShowLoadingDataGif:YES];
}
II iOS实现无感知上拉加载更多
2.1 思路1:UITableViewDataSourcePrefetching
// this protocol can provide information about cells before they are displayed on screen.
@protocol UITableViewDataSourcePrefetching <NSObject>
@required
// indexPaths are ordered ascending by geometric distance from the table view
- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
@optional
// indexPaths that previously were considered as candidates for pre-fetching, but were not actually used; may be a subset of the previous call to -tableView:prefetchRowsAtIndexPaths:
- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
@end
2.2 实现思路2:通过 KVO 去监听 scrollView 的 contentOffset 变化
MJRefreshAutoFooter 有个专门的属性triggerAutomaticallyRefreshPercent 去做自动刷新
#import "MJRefreshFooter.h"
NS_ASSUME_NONNULL_BEGIN
@interface MJRefreshAutoFooter : MJRefreshFooter
/** 是否自动刷新(默认为YES) */
@property (assign, nonatomic, getter=isAutomaticallyRefresh) BOOL automaticallyRefresh;
/** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
@property (assign, nonatomic) CGFloat appearencePercentTriggerAutoRefresh MJRefreshDeprecated("请使用triggerAutomaticallyRefreshPercent属性");
/** 当底部控件出现多少时就自动刷新(默认为1.0,也就是底部控件完全出现时,才会自动刷新) */
@property (assign, nonatomic) CGFloat triggerAutomaticallyRefreshPercent;
/** 自动触发次数, 默认为 1, 仅在拖拽 ScrollView 时才生效,
如果为 -1, 则为无限触发
*/
@property (nonatomic) NSInteger autoTriggerTimes;
@end
III 刷新控件的适配
- 上拉加载:安全区域距离适配
#define k_safeAreaInsetsBottom [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom
#define isIphoneX isHasSafeAreaInsets
#define k_ignoredScrollViewContentInsetBottom (isIphoneX?k_safeAreaInsetsBottom:0)
_vcView.tableView.mj_footer.ignoredScrollViewContentInsetBottom = k_ignoredScrollViewContentInsetBottom;
-
下拉刷新适配:present 半屏适配、设置下拉样式
blog.csdn.net/z929118967/… -
分页并发适配:
方式1. 升级MJRefresh到3.7.5版本
Fix/duplicated async method -> Installing MJRefresh 3.7.5 (was 3.3.1)
方式2. 使用自动刷新控件MJRefreshNormalHeader->MJRefreshAutoNormalFooter
see also
案例:新浪微博API(获取用户微博数据)
download.csdn.net/download/u0…
- 集成下拉刷新控件:下拉刷新 HWHomeTableViewController
- 获取未读消息数: HWHomeTableViewController
- 封装标题按钮:HWTitleButton
更多内容请关注公众号:iOS逆向
© 版权声明
文章版权归作者所有,未经允许请勿转载,侵权请联系 admin@trc20.tw 删除。
THE END