之前做了一个使用UITableViewController中实现刷新的方法,这里实现一个使用UIViewController实现下拉刷新,当果然要使用到UIRefreshControl。
创建啥的我就不记录了,文件列一下
MeCelebrityViewController.m
MeCelebrityViewController.h
MeCelebrityViewController.xib
下面列出主要的实现过程
第一步:组件的添加
1
@property(nonatomic,strong)UIRefreshControl*refreshControl;
viewDidLoad的初始化
1
2
3
4
5
6
7
//添加刷新
_refreshControl=[[UIRefreshControlalloc]init];
[_refreshControladdTarget:self
action:@selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
[_refreshControlsetAttributedTitle:[[NSAttributedStringalloc]initWithString:@"松手更新数据"]];
[_dataTableViewaddSubview:_refreshControl];
这里的_dataTableView是要自己添加的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
CGFloattableViewX=0.0;
CGFloattableViewY=0.0;
CGFloattableWidth=self.navigationController.navigationBar.frame.size.width;
CGFloattableHeight=[[UIScreenmainScreen]bounds].size.height;
_dataTableView=[[UITableViewalloc]initWithFrame:CGRectMake(tableViewX,
tableViewY,
tableWidth,
tableHeight)
style:UITableViewStylePlain];
_cellHeight=70.0;
_dataTableView.delegate=self;
_dataTableView.dataSource=self;
[self.viewaddSubview:_dataTableView];
第二步:实现下拉刷新的动作
1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)refreshView:(UIRefreshControl*)refresh
{
refresh.attributedTitle=[[NSAttributedStringalloc]initWithString:@"更新数据中..."];
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetDateFormat:@"MMMd,h:mma"];
NSString*lastUpdated=[NSStringstringWithFormat:@"上次更新日期%@",
[formatterstringFromDate:[NSDatedate]]];
refresh.attributedTitle=[[NSAttributedStringalloc]initWithString:lastUpdated];
[selfinitData];
[_dataTableViewreloadData];
[refreshendRefreshing];
}
结果不是很完美,因为有遮挡的现象,不过流程走通了,下面开始自己的制作吧.
UIRefreshControl的使用非常简单。
1、使用范围
如果你装了xcode_4.5_developer_preview,那么在UITableViewController.h文件中你会看到,UITableViewController里面有如下声明,说明UITableViewController已经内置了UIRefreshControl控件
[cpp]view plaincopy
@property(nonatomic,retain)UIRefreshControl*refreshControlNS_AVAILABLE_IOS(6_0);
【注】:UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行时会得到如下错误提示:(即UIRefreshControl只能被UITableViewController管理)
[cpp]view plaincopy
-06-1514:34:34.908DevDivUIRefreshControl[722:10103]***Terminatingappduetouncaughtexception"NSInternalInconsistencyException",reason:"UIRefreshControlmayonlybemanagedbyaUITableViewController"
***Firstthrowcallstack:
(0x186fd720x1066e510x186fb4b0x55a5590x572380x5d4820x55ad20x2ebb0xeb2a30xeb30e0x10b7e90x10b6240x109aef0x10999c0x107adc0x1082c60xecf240xed1e00xee0840x5645c0x5cf310x55ad20x4131d0x414f60x4168c0x498710x10a900x1196a0x222be0x22f9f0x153fd0x17ccf390x17ccc100x17e5da50x17e5b120x1816b460x1815ed40x1815dab0x1128f0x12e710x29fd0x2925)
libc++abi.dylib:terminatecalledthrowinganexception
(lldb)
2、如何使用
a)初始化
如何在UITableViewController 中使用UIRefreshControl呢,在上面给出的代码附件中,你可以很详细的知道,这里介绍一下关键的部分:
[cpp]view plaincopy
self.refreshControl=[[UIRefreshControlalloc]init];
//self.refreshControl.tintColor=[UIColorblueColor];
self.refreshControl.attributedTitle=[[NSAttributedStringalloc]initWithString:@"下拉刷新"];
[self.refreshControladdTarget:selfaction:@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 plaincopy
[selfperformSelector:@selector(handleData)withObject:nilafterDelay:2];
在handleData里面,就表示已经请求到了数据,在此进行UI更新即可。也需要注意的是,我们调用UIRefreshControl 的endRefreshing方法,表示刷新结束,让UIRefreshControl更新显示。
[cpp]view plaincopy
-(void)handleData
{
NSLog(@"refreshed");
[self.refreshControlendRefreshing];
self.refreshControl.attributedTitle=[[NSAttributedStringalloc]initWithString:@"下拉刷新"];
self.count++;
[self.tableViewreloadData];
}
3、官方头文件
下面是sdk中UIRefreshControl的声明,想必看了下面的代码,你已经知道如何使用了。
[cpp]view plaincopy
//
//UIRefreshControl.h
//UIKit
//
//CopyrightAppleInc.Allrightsreserved.
//
#import<Foundation/Foundation.h>
#import<UIKit/UIControl.h>
#import<UIKit/UIKitDefines.h>
NS_CLASS_AVAILABLE_IOS(6_0)@interfaceUIRefreshControl:UIControl
/*Thedesignatedinitializer
*ThisinitializesaUIRefreshControlwithadefaultheightandwidth.
*OnceassignedtoaUITableViewController,theframeofthecontrolismanagedautomatically.
*Whenauserhaspulled-to-refresh,theUIRefreshControlfiresitsUIControlEventValueChangedevent.
*/
-(id)init;
@property(nonatomic,readonly,getter=isRefreshing)BOOLrefreshing;
@property(nonatomic,retain)UIColor*tintColorUI_APPEARANCE_SELECTOR;
@property(nonatomic,retain)NSAttributedString*attributedTitleUI_APPEARANCE_SELECTOR;
//MaybeusedtoindicatetotherefreshControlthatanexternaleventhasinitiatedtherefreshaction
-(void)beginRefreshingNS_AVAILABLE_IOS(6_0);
//Mustbeexplicitlycalledwhentherefreshinghascompleted
-(void)endRefreshingNS_AVAILABLE_IOS(6_0);
@end