在iOS開(kāi)發(fā)中翁授,我們經(jīng)常要用到下拉刷新和上拉刷新來(lái)加載新的數(shù)據(jù)残拐,當(dāng)前這也適合分頁(yè)涎拉。iOS原生就帶有該方法瑞侮,下面就iOS自帶的下拉刷新方法來(lái)簡(jiǎn)單操作。
上拉刷新
1鼓拧、在TableView里半火,一打開(kāi)軟件,我們就調(diào)用下拉刷新事件季俩。
24- (void)viewDidLoad {
[superviewDidLoad];
// 集成刷新控件
[selfsetupRefresh];
}
/**
*? 集成下拉刷新
*/
-(void)setupRefresh
{
//1.添加刷新控件
UIRefreshControl *control=[[UIRefreshControl alloc]init];
[control addTarget:selfaction:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:control];
//2.馬上進(jìn)入刷新?tīng)顟B(tài)钮糖,并不會(huì)觸發(fā)UIControlEventValueChanged事件
[control beginRefreshing];
// 3.加載數(shù)據(jù)
[selfrefreshStateChange:control];
}
2、接下來(lái)酌住,我們就要實(shí)現(xiàn)?refreshStateChange 這個(gè)方法店归,在里面顯示數(shù)據(jù)和關(guān)閉下拉刷新。
*? UIRefreshControl進(jìn)入刷新?tīng)顟B(tài):加載最新的數(shù)據(jù)
*/
-(void)refreshStateChange:(UIRefreshControl *)control
{
// 3.發(fā)送請(qǐng)求
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
[mgr GET:@"https://api.weibo.com/2/statuses/public_timeline.json"parameters:nilsuccess:^(AFHTTPRequestOperation *operation,NSDictionary*responseObject){
//1.獲取數(shù)據(jù)酪我,處理數(shù)據(jù)消痛,傳遞數(shù)據(jù)給tableView,如:
// 將最新的微博數(shù)據(jù),添加到總數(shù)組的最前面
//??????? NSRange range = NSMakeRange(0, newStatuses.count);
//??????? NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
//??????? [self.statuses insertObjects:newStatuses atIndexes:set];
//2.刷新表格
[self.tableView reloadData];
// 3. 結(jié)束刷新
[control endRefreshing];
} failure:^(AFHTTPRequestOperation *operation,NSError*error) {
// 結(jié)束刷新刷新 都哭,為了避免網(wǎng)絡(luò)加載失敗秩伞,一直顯示刷新?tīng)顟B(tài)的錯(cuò)誤
[control endRefreshing];
}];
}
上拉刷新
上拉刷新,一般用于分頁(yè)請(qǐng)求欺矫,拉到底后稠歉,自動(dòng)加載下一頁(yè)。下面就拿加載新浪微博數(shù)據(jù)為例汇陆。
一怒炸、由于下載加載更多數(shù)據(jù),是一個(gè)不變的布局控件毡代,我們就用xib來(lái)實(shí)現(xiàn)阅羹。
HWLoadMoreFooter.h
5#import
@interfaceHWLoadMoreFooter : UIView
+(instancetype)footer;
@end
HWLoadMoreFooter.m
10#import "HWLoadMoreFooter.h"
@implementationHWLoadMoreFooter
+(instancetype)footer
{
return[[[NSBundlemainBundle] loadNibNamed:@"HWLoadMoreFooter"owner:niloptions:nil] lastObject];
}
@end
接著,我們建立一個(gè)名為HWLoadMoreFooter的xib
接下來(lái)教寂,需要設(shè)置下面三個(gè)地方:
接著在框里拖拉一個(gè)Label捏鱼,設(shè)置Label為填充整個(gè)view
最后,點(diǎn)擊下圖紅色框酪耕,Update Frames
xib建好之后导梆,下面我們來(lái)實(shí)現(xiàn)上拉刷新的代碼
二.實(shí)現(xiàn)代碼。
1.在TabelView中加載時(shí),先加載該控件
10- (void)viewDidLoad {
[superviewDidLoad];
// 集成下拉刷新控件
[selfsetupUpRefresh];
// 集成上拉刷新控件
[selfsetupDownRefresh];
}
2.集成上拉刷新方法
/**
*? 集成上拉刷新
*/
-(void)setupDownRefresh
{
HWLoadMoreFooter *footer = [HWLoadMoreFooter footer];
footer.hidden =YES;
self.tableView.tableFooterView = footer;
}
3.異步請(qǐng)求數(shù)據(jù)方法
39- (void)loadMoreStatus
{
// 1.請(qǐng)求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.拼接請(qǐng)求參數(shù)
HWAccount *account = [HWAccountTool account];
NSMutableDictionary*params = [NSMutableDictionarydictionary];
params[@"access_token"] = account.access_token;
// 取出最后面的微博(最新的微博看尼,ID最大的微博)
HWStatus *lastStatus = [self.statuses lastObject];
if(lastStatus) {
// 若指定此參數(shù)递鹉,則返回ID小于或等于max_id的微博,默認(rèn)為0藏斩。
// id這種數(shù)據(jù)一般都是比較大的躏结,一般轉(zhuǎn)成整數(shù)的話(huà),最好是long long類(lèi)型
longlongmaxId = lastStatus.idstr.longLongValue - 1;
params[@"max_id"] = @(maxId);
}
// 3.發(fā)送請(qǐng)求
[mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json"parameters:params success:^(AFHTTPRequestOperation *operation,NSDictionary*responseObject) {
// 將 "微博字典"數(shù)組 轉(zhuǎn)為 "微博模型"數(shù)組
NSArray*newStatuses = [HWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
// 將更多的微博數(shù)據(jù)狰域,添加到總數(shù)組的最后面
[self.statuses addObjectsFromArray:newStatuses];
// 刷新表格
[self.tableView reloadData];
// 結(jié)束刷新(隱藏footer)
self.tableView.tableFooterView.hidden =YES;
} failure:^(AFHTTPRequestOperation *operation,NSError*error) {
HWLog(@"請(qǐng)求失敗-%@", error);
// 結(jié)束刷新
self.tableView.tableFooterView.hidden =YES;
}];
}
4.實(shí)現(xiàn)scrollViewDidScroll
18- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//??? scrollView == self.tableView == self.view
// 如果tableView還沒(méi)有數(shù)據(jù)媳拴,就直接返回
if(self.statuses.count == 0 ||self.tableView.tableFooterView.isHidden ==NO)return;
CGFloat offsetY = scrollView.contentOffset.y;
// 當(dāng)最后一個(gè)cell完全顯示在眼前時(shí),contentOffset的y值
CGFloat judgeOffsetY = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.height -self.tableView.tableFooterView.height;
if(offsetY >= judgeOffsetY) {// 最后一個(gè)cell完全進(jìn)入視野范圍內(nèi)
// 顯示footer
self.tableView.tableFooterView.hidden =NO;
// 加載更多的微博數(shù)據(jù)
[selfloadMoreStatus];
}
}