iOS开发过程中 可能会遇到下拉刷新的问题
今天我们演示一下下拉刷新的效果
首先 新建项目 起名 TestRefreshControl
UIRefreshControl 只在UITableView上起作用
我们直接在默认生成的ViewController中新建TableView 同时创建一个 UIRefreshControl@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *testTableView;
UIRefreshControl *testRefreshControl;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建tableView 添加到当前View上
testTableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
testTableView.dataSource=self;
testTableView.delegate=self;
[self.view addSubview:testTableView];
//创建testRefreshControl添加到testTableView 上
testRefreshControl = [[UIRefreshControl alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[testRefreshControl setAttributedTitle:[[NSAttributedString alloc] initWithString:@"松手更新数据"]];
[testTableView addSubview:testRefreshControl];
[testRefreshControl addTarget:self action:@selector(headerRereshing:) forControlEvents:UIControlEventValueChanged];
}
实现 下拉刷新
//下拉tableView 就会出发次方法
-(void)headerRereshing:(UIRefreshControl *)refresh{
//当下拉刷新的时候我们一般会获取数据更新tableView 这里我们就简单模拟一下 让程序睡眠1秒 之后 停止刷新
[NSThread sleepForTimeInterval:1.0f];
[refresh endRefreshing];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text=[NSString stringWithFormat:@"第%ld行",indexPath.row];
return cell;
}
好 我们可以测试一下
效果如下
好了 源代码 我会上传到群空间 大家有兴趣可以去下载
demo:【60325下拉刷新RefreshControl.zip】
苹果开发群 :414319235 欢迎加入,共同学习