在iOS開發中,我們經常要用到下拉刷新和上拉刷新來加載新的數據,當前這也適合分頁。iOS原生就帶有該方法,下面就iOS自帶的下拉刷新方法來簡單操作。
上拉刷新
1、在TableView里,一打開軟件,我們就調用下拉刷新事件。
- (void)viewDidLoad { [super viewDidLoad]; // 集成刷新控件 [self setupRefresh]; } /** * 集成下拉刷新 */ -(void)setupRefresh { //1.添加刷新控件 UIRefreshControl *control=[[UIRefreshControl alloc]init]; [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged]; [self.tableView addSubview:control]; //2.馬上進入刷新狀態,並不會觸發UIControlEventValueChanged事件 [control beginRefreshing]; // 3.加載數據 [self refreshStateChange:control]; }
2、接下來,我們就要實現 refreshStateChange 這個方法,在里面顯示數據和關閉下拉刷新。
/** * UIRefreshControl進入刷新狀態:加載最新的數據 */ -(void)refreshStateChange:(UIRefreshControl *)control { // 3.發送請求 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; [mgr GET:@"https://api.weibo.com/2/statuses/public_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject){ //1.獲取數據,處理數據,傳遞數據給tableView,如: // 將最新的微博數據,添加到總數組的最前面 // NSRange range = NSMakeRange(0, newStatuses.count); // NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range]; // [self.statuses insertObjects:newStatuses atIndexes:set]; //2.刷新表格 [self.tableView reloadData]; // 3. 結束刷新 [control endRefreshing]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 結束刷新刷新 ,為了避免網絡加載失敗,一直顯示刷新狀態的錯誤 [control endRefreshing]; }]; }
上拉刷新
上拉刷新,一般用於分頁請求,拉到底后,自動加載下一頁。下面就拿加載新浪微博數據為例。
一、由於下載加載更多數據,是一個不變的布局控件,我們就用xib來實現。
HWLoadMoreFooter.h
#import <UIKit/UIKit.h> @interface HWLoadMoreFooter : UIView +(instancetype)footer; @end
HWLoadMoreFooter.m
#import "HWLoadMoreFooter.h" @implementation HWLoadMoreFooter +(instancetype)footer { return [[[NSBundle mainBundle] loadNibNamed:@"HWLoadMoreFooter" owner:nil options:nil] lastObject]; } @end
接着,我們建立一個名為HWLoadMoreFooter的xib
接下來,需要設置下面三個地方:
接着在框里拖拉一個Label,設置Label為填充整個view
最后,點擊下圖紅色框,Update Frames
xib建好之后,下面我們來實現上拉刷新的代碼
二.實現代碼。
1.在TabelView中加載時,先加載該控件
- (void)viewDidLoad { [super viewDidLoad]; // 集成下拉刷新控件 [self setupUpRefresh]; // 集成上拉刷新控件 [self setupDownRefresh]; }
2.集成上拉刷新方法
/** * 集成上拉刷新 */ -(void)setupDownRefresh { HWLoadMoreFooter *footer = [HWLoadMoreFooter footer]; footer.hidden = YES; self.tableView.tableFooterView = footer; }
3.異步請求數據方法
- (void)loadMoreStatus { // 1.請求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // 2.拼接請求參數 HWAccount *account = [HWAccountTool account]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"access_token"] = account.access_token; // 取出最后面的微博(最新的微博,ID最大的微博) HWStatus *lastStatus = [self.statuses lastObject]; if (lastStatus) { // 若指定此參數,則返回ID小於或等於max_id的微博,默認為0。 // id這種數據一般都是比較大的,一般轉成整數的話,最好是long long類型 long long maxId = lastStatus.idstr.longLongValue - 1; params[@"max_id"] = @(maxId); } // 3.發送請求 [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) { // 將 "微博字典"數組 轉為 "微博模型"數組 NSArray *newStatuses = [HWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]]; // 將更多的微博數據,添加到總數組的最后面 [self.statuses addObjectsFromArray:newStatuses]; // 刷新表格 [self.tableView reloadData]; // 結束刷新(隱藏footer) self.tableView.tableFooterView.hidden = YES; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { HWLog(@"請求失敗-%@", error); // 結束刷新 self.tableView.tableFooterView.hidden = YES; }]; }
4.實現scrollViewDidScroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // scrollView == self.tableView == self.view // 如果tableView還沒有數據,就直接返回 if (self.statuses.count == 0 || self.tableView.tableFooterView.isHidden == NO) return; CGFloat offsetY = scrollView.contentOffset.y; // 當最后一個cell完全顯示在眼前時,contentOffset的y值 CGFloat judgeOffsetY = scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.height - self.tableView.tableFooterView.height; if (offsetY >= judgeOffsetY) { // 最后一個cell完全進入視野范圍內 // 顯示footer self.tableView.tableFooterView.hidden = NO; // 加載更多的微博數據 [self loadMoreStatus]; } }