iOS 上啦下拉刷新

在iOS開發中,咱們常常要用到下拉刷新和上拉刷新來加載新的數據,當前這也適合分頁。iOS原生就帶有該方法,下面就iOS自帶的下拉刷新方法來簡單操做。json

上拉刷新

一、在TableView裏,一打開軟件,咱們就調用下拉刷新事件。api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- ( 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];
}

二、接下來,咱們就要實現 refreshStateChange 這個方法,在裏面顯示數據和關閉下拉刷新。數組

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
  *  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];
     }];
}

上拉刷新 

上拉刷新,通常用於分頁請求,拉到底後,自動加載下一頁。下面就拿加載新浪微博數據爲例。網絡

1、因爲下載加載更多數據,是一個不變的佈局控件,咱們就用xib來實現。異步

HWLoadMoreFooter.h佈局

1
2
3
4
5
#import <UIKit/UIKit.h>
 
@interface HWLoadMoreFooter : UIView
+(instancetype)footer;
@end

HWLoadMoreFooter.mspa

1
2
3
4
5
6
7
8
9
10
#import "HWLoadMoreFooter.h"
 
@implementation HWLoadMoreFooter
 
+(instancetype)footer
{
     return [[[ NSBundle mainBundle] loadNibNamed:@ "HWLoadMoreFooter" owner: nil options: nil ] lastObject];
}
 
@end

接着,咱們創建一個名爲HWLoadMoreFooter的xibcode

接下來,須要設置下面三個地方:blog

接着在框裏拖拉一個Label,設置Label爲填充整個viewtoken

最後,點擊下圖紅色框,Update Frames

 

xib建好以後,下面咱們來實現上拉刷新的代碼

二.實現代碼。

1.在TabelView中加載時,先加載該控件

1
2
3
4
5
6
7
8
9
10
- ( void )viewDidLoad {
     [ super viewDidLoad];
     
     // 集成下拉刷新控件
     [ self setupUpRefresh];
     
     // 集成上拉刷新控件
     [ self setupDownRefresh];
     
}

 2.集成上拉刷新方法

1
2
3
4
5
6
7
8
9
/**
  *  集成上拉刷新
  */
-( void )setupDownRefresh
{
     HWLoadMoreFooter *footer = [HWLoadMoreFooter footer];
     footer.hidden = YES ;
     self .tableView.tableFooterView = footer;
}

 3.異步請求數據方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- ( 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- ( 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];
     }
}
相關文章
相關標籤/搜索