首先定义一个刷新控件:
@property (nonatomic, strong)UIRefreshControl *refresh;
_refresh = [[UIRefreshControl alloc] init];
设置属性与添加控件:
//设置提示标题
_refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[self.table addSubview:_refresh];
[_refresh addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
刷新方法:
-(void) refreshData{
//设置两秒延迟模拟远程获取数据
[self performSelector:@selector(handleData) withObject:nil afterDelay:2];
}
-(void) handleData{
NSString *randStr = [NSString stringWithFormat:@"%d",arc4random() % 1000];//随机获取一个数字
[_list addObject:randStr];
_refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新..."];
[_refresh endRefreshing];
[_table reloadData];
}
效果图如下
该事件处理方法适用于从底层数据库,网络,远程应用等地方检索加载数据,并将之显示在TableViewCell上。