引言
需求:
1、iOS零售版ERP APP增加支付獎勵消息通知
2、通知信息(定時xx點;歷史消息可查) 2021-04-29 尊敬的商家ios上拉加載更多,您參與的xxx激勵金活動ios上拉加載更多,昨日參與成功10筆,共獲得激勵金1元!
由于消息列表,數據量比較大,為了提升用戶體驗,需采用分頁加載顯示數據
I、集成下拉刷新控件
在這里插入圖片描述 1.1 定義相關分頁屬性
@property?(nonatomic?,?assign)?NSInteger?pageNum;//當前頁碼
@property?(nonatomic?,?assign)?NSInteger?pageCount;//?總頁數
@property?(nonatomic?,?assign)?BOOL?isfooterRereshing;
//?每頁顯示數...
@property?(nonatomic,strong)??NSMutableArray?*datas;
@property?(nonatomic,strong)??RACSubject?*reloadSubject;
@property?(nonatomic,strong)??RACSubject?*ShowNoviewSubject;
@property?(nonatomic,strong)??RACSubject?*hidenNoviewSubject;
1.2 監聽下拉和上拉事件
????????_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];
????????
完成代碼請看原文:
II、iOS實現無感知上拉加載更多
//?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
其他實現思路:通過 KVO 去監聽 的 變化
有個專門的屬性 去做自動刷新
#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 、案例:新浪微博API(獲取用戶微博數據)
see also