IOS7 UIViewController中使用UIRefreshControl 实现 UITableView下拉刷新

5 篇文章 0 订阅
3 篇文章 0 订阅

之前做了一个使用UITableViewController中实现刷新的方法,这里实现一个使用UIViewController实现下拉刷新,当果然要使用到UIRefreshControl。

创建啥的我就不记录了,文件列一下

MeCelebrityViewController.m

MeCelebrityViewController.h

MeCelebrityViewController.xib

下面列出主要的实现过程

第一步:组件的添加

1
@property (nonatomic, strong) UIRefreshControl* refreshControl;

viewDidLoad的初始化

1
2
3
4
5
6
7
//添加刷新
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self
                   action:@selector(refreshView:)
         forControlEvents:UIControlEventValueChanged];
[_refreshControl setAttributedTitle:[[NSAttributedString alloc] initWithString:@ "松手更新数据" ]];
[_dataTableView addSubview:_refreshControl];

这里的_dataTableView是要自己添加的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CGFloat tableViewX = 0.0;
CGFloat tableViewY = 0.0;
CGFloat tableWidth = self.navigationController.navigationBar.frame.size.width;
CGFloat tableHeight = [[UIScreen mainScreen] bounds].size.height;
_dataTableView = [[UITableView alloc] initWithFrame:CGRectMake(tableViewX,
                                                                tableViewY,
                                                                tableWidth,
                                                                tableHeight)
                                               style:UITableViewStylePlain];
 
_cellHeight = 70.0;
_dataTableView.delegate = self;
_dataTableView.dataSource = self;
[self.view addSubview:_dataTableView];

第二步:实现下拉刷新的动作

1
2
3
4
5
6
7
8
9
10
11
12
13
-( void ) refreshView:(UIRefreshControl *)refresh
{
     refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@ "更新数据中..." ];
     
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setDateFormat:@ "MMM d, h:mm a" ];
     NSString *lastUpdated = [NSString stringWithFormat:@ "上次更新日期 %@" ,
                              [formatter stringFromDate:[NSDate date]]];
     refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
     [self initData];
     [_dataTableView reloadData];
     [refresh endRefreshing];
}

结果不是很完美,因为有遮挡的现象,不过流程走通了,下面开始自己的制作吧.


UIRefreshControl的使用非常简单。

1、使用范围
如果你装了xcode_4.5_developer_preview,那么在UITableViewController.h文件中你会看到,UITableViewController里面有如下声明,说明UITableViewController已经内置了UIRefreshControl控件

[cpp]  view plain copy
  1. @property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);  

【注】:UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会得到如下错误提示:(即UIRefreshControl只能被UITableViewController管理)

[cpp]  view plain copy
  1. 2012-06-15 14:34:34.908 DevDivUIRefreshControl[722:10103] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIRefreshControl may only be managed by a UITableViewController'  
  2. *** First throw call stack:  
  3. (0x186fd72 0x1066e51 0x186fb4b 0x55a559 0x57238 0x5d482 0x55ad2 0x2ebb 0xeb2a3 0xeb30e 0x10b7e9 0x10b624 0x109aef 0x10999c 0x107adc 0x1082c6 0xecf24 0xed1e0 0xee084 0x5645c 0x5cf31 0x55ad2 0x4131d 0x414f6 0x4168c 0x49871 0x10a90 0x1196a 0x222be 0x22f9f 0x153fd 0x17ccf39 0x17ccc10 0x17e5da5 0x17e5b12 0x1816b46 0x1815ed4 0x1815dab 0x1128f 0x12e71 0x29fd 0x2925)  
  4. libc++abi.dylib: terminate called throwing an exception  
  5. (lldb)   

2、如何使用
    a)初始化
如何在UITableViewController 中使用UIRefreshControl呢,在上面给出的代码附件中,你可以很详细的知道,这里介绍一下关键的部分:

[cpp]  view plain copy
  1.    self.refreshControl = [[UIRefreshControl alloc]init];  
  2.     //    self.refreshControl.tintColor = [UIColor blueColor];  
  3.     self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];  
  4.     [self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged) forControlEvents:UIControlEventValueChanged];  

如上面看到的代码,虽然UITableViewController已经声明了UIRefreshControl,但是貌似还没有初始化,所以需要我们自己初始化。很神奇,初始化的时候并不需要给它指定frame,UITableViewController会为我们进行管理。遗憾的时目前只看到下拉刷新功能,上拉刷新还没有,估计在最终版里面苹果会考虑加入上拉刷新功能。
我们还可以给UIRefreshControl设置tintColor和attributedTitle。

    b)下拉刷新事件监听
当用户进行下拉刷新操作时,UIRefreshControl 会触发一个UIControlEventValueChanged事件,通过监听这个事件,我们就可以进行类似数据请求的操作了。如下代码:
[self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged)


c)进行数据请求
在示例中,为了演示数据请求,我简单的做了一个延时处理,2秒钟后,调用handleData

[cpp]  view plain copy
  1. [self performSelector:@selector(handleData) withObject:nil afterDelay:2];  
在handleData里面,就表示已经请求到了数据,在此进行UI更新即可。也需要注意的是,我们调用UIRefreshControl 的endRefreshing方法,表示刷新结束,让UIRefreshControl更新显示。

[cpp]  view plain copy
  1. - (void) handleData  
  2. {  
  3.     NSLog(@"refreshed");  
  4.     [self.refreshControl endRefreshing];  
  5.     self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];  
  6.   
  7.     self.count++;  
  8.     [self.tableView reloadData];  
  9. }  

3、官方头文件
下面是sdk中UIRefreshControl的声明,想必看了下面的代码,你已经知道如何使用了。

[cpp]  view plain copy
  1. //  
  2. //  UIRefreshControl.h  
  3. //  UIKit  
  4. //  
  5. //  Copyright 2012 Apple Inc. All rights reserved.  
  6. //  
  7.   
  8. #import <Foundation/Foundation.h>  
  9. #import <UIKit/UIControl.h>  
  10. #import <UIKit/UIKitDefines.h>  
  11.   
  12. NS_CLASS_AVAILABLE_IOS(6_0) @interface UIRefreshControl : UIControl  
  13.   
  14. /* The designated initializer 
  15.  * This initializes a UIRefreshControl with a default height and width. 
  16.  * Once assigned to a UITableViewController, the frame of the control is managed automatically. 
  17.  * When a user has pulled-to-refresh, the UIRefreshControl fires its UIControlEventValueChanged event. 
  18.  */  
  19. - (id)init;  
  20.   
  21. @property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing;  
  22.   
  23. @property (nonatomic, retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;  
  24. @property (nonatomic, retain) NSAttributedString *attributedTitle UI_APPEARANCE_SELECTOR;  
  25.   
  26. // May be used to indicate to the refreshControl that an external event has initiated the refresh action  
  27. - (void)beginRefreshing NS_AVAILABLE_IOS(6_0);  
  28. // Must be explicitly called when the refreshing has completed  
  29. - (void)endRefreshing NS_AVAILABLE_IOS(6_0);  
  30.   
  31. @end  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值