要使用该控件首先必须继承 UITableViewController
1、初始化控件
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
UIRefreshControl *fresh=[[UIRefreshControl alloc]init];
[fresh addTarget:self action:@selector(freshAction:) forControlEvents:UIControlEventValueChanged];
fresh.attributedTitle=[[NSMutableAttributedString alloc]initWithString:@"下拉刷新"];
self.refreshControl=fresh;
[fresh release];
}
return self;
}
-(void)freshAction:(UIRefreshControl*)aRefresh
{
self.refreshControl.attributedTitle=[[NSMutableAttributedString alloc]initWithString:@"加载中..."];
[self.refreshControl beginRefreshing];
//模拟多线程请求
[self performSelectorInBackground:@selector(requestData:) withObject:nil];
}
-(void)requestData:(id)sender
{
sleep(1);
[self performSelectorOnMainThread:@selector(reloadUI) withObject:nil waitUntilDone:NO];
}
-(void)reloadUI
{
self.refreshControl.attributedTitle=[[[NSMutableAttributedString alloc]initWithString:@"下拉可以刷新"]autorelease];
[self.refreshControl endRefreshing];
[self.tableView reloadData];
}