iOS经典讲解之实现上拉刷新和下拉刷新


在iOS开发中,我们经常要用到下拉刷新和上拉刷新来加载新的数据,当前这也适合分页。iOS原生就带有该方法,下面就iOS自带的下拉刷新方法来简单操作。

上拉刷新

1、在TableView里,一打开软件,我们就调用下拉刷新事件。

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];
}

2、接下来,我们就要实现 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];
     }];
}

上拉刷新 

上拉刷新,一般用于分页请求,拉到底后,自动加载下一页。下面就拿加载新浪微博数据为例。

一、由于下载加载更多数据,是一个不变的布局控件,我们就用xib来实现。

HWLoadMoreFooter.h

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

HWLoadMoreFooter.m

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的xib

接下来,需要设置下面三个地方:

接着在框里拖拉一个Label,设置Label为填充整个view

最后,点击下图红色框,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];
     }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值