ios应用里需要用到下拉刷新来更新应用的数据,ios自带的下拉刷新UIRefreshControl目前只能用于UITableViewController
-(void)beginRefreshing //创建下拉刷新
{
refresh = [[UIRefreshControl alloc]init];
refresh.tintColor = [UIColor lightGrayColor];
refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"]; //属性名称为下拉刷新
[refresh addTarget:self action:@selector(refreshTabelviewAction:) forControlEvents:UIControlEventValueChanged]; //添加事件
self.refreshControl = refresh;
}
-(void)refreshTabelviewAction:(UIRefreshControl *)refreshs
{
if(refreshs.refreshing)//正在刷新的时候的显示
{
refreshs.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
[self performSelector:@selector(refreshData) withObject:nil afterDelay:2];
}
}
-(void)refreshData
{
NSString *syseTime = nil;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-mm-dd hh:mm:ss" ];
syseTime = [formatter stringFromDate:[NSDate date]];
NSString *lastUpdated = [NSString stringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated] ;//以上为刷新时显示上一次刷新的时间
[self.refreshControl endRefreshing];//结束刷新
[self.tableView reloadData];//重新加载数据
}