需要包装viewController视图
viewController.m代码全部粘贴即可使用
//
// ViewController.m
// 练习 刷新数据
//
// Created by qianfeng on 16/2/24.
// Copyright © 2016年 mc. All rights reserved.
// QQ 937496430
#define SCR_W self.view.frame.size.width
#define SCR_H self.view.frame.size.height
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView * tableView;
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic)NSInteger currentPage; //当前页数
@property (nonatomic)NSInteger refreshState; //0初始状态 //1已经上拉 需要松手 //2正在获取数据状态
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
[self createTable];
}
- (void)loadData
{
if(_dataArray == nil)
{
_dataArray = [[NSMutableArray alloc] init];
}
for (int index = 0; index < 20; index++) {
NSString *str = [NSString stringWithFormat:@"第%ld页第%d个Cell",_currentPage,index];
[_dataArray addObject:str];
}
_currentPage++;
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
_refreshState = 0;
[self updateRefreshView];
});
}
- (void)createTable
{
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H)];
[self.view addSubview:_tableView];
_tableView.tableFooterView = [self createFooterView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
// 创建footer视图 返回UIView
- (UIView*)createFooterView
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
[view setBackgroundColor:[UIColor whiteColor]];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
[lable setTextAlignment:NSTextAlignmentCenter];
[lable setText:@"上拉刷新"];
[lable setTextColor:[UIColor darkGrayColor]];
lable.tag = 100;
[view addSubview:lable];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL_ID"];
}
cell.backgroundColor = [UIColor grayColor];
cell.textLabel.text = _dataArray[indexPath.row];
return cell;
}
//刷新数据
- (void)updateRefreshView
{
UIView *refreshV = _tableView.tableFooterView;
UILabel *titleL = (UILabel*)[refreshV viewWithTag:100];
if(_refreshState == 0)
{
[titleL setText:@"上拉刷新数据"];
}else if(_refreshState == 1){
[titleL setText:@"松手即刷新数据"];
}else if(_refreshState == 2){
[titleL setText:@"正在下载数据..."];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ((_refreshState == 0)&&(scrollView.contentOffset.y + _tableView.frame.size.height > scrollView.contentSize.height + 50)) {
//scrollView.contentSize.height 加 50 是为了防止滑动过快 导致刷新
_refreshState = 1;
[self updateRefreshView];
}
}
//滚动视图已经结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if(_refreshState == 1)
{
_refreshState = 2;
[self updateRefreshView];
[self performSelector:@selector(loadData) withObject:nil afterDelay:3]; //延迟3秒 为了更直观的显示 _refreshState 的状态
// [self loadData];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end