iOS和android中经常会有下拉刷新的页面,这里用EGOTableViewRefresh开源库来实现iOS中的下拉刷新
1.到这里下载EGOTableViewRefresh开源库
2.新建一个single view项目,将上边的库导入
3.导入系统库QuartzCore,如图:
4.上代码,ViewController.h
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, EGORefreshTableHeaderDelegate> {
UITableView* _tableView;
NSMutableArray* _dataArray;//数据数组
EGORefreshTableHeaderView* _refreshView;//刷新视图
BOOL _isRefresh;//是否刷新
}
@end
_dataArray = [[NSMutableArray alloc] initWithObjects:@"data1", @"data2", @"data3", @"data4", @"data5", nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView release];
_refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, -460, 320, 460)];
_refreshView.delegate = self;//设置刷新代理
[_tableView addSubview:_refreshView];//将刷新视图添加到tableView上边
[_refreshView release];
6.EGOTableViewRefreshDelegate代理中有三个方法,前两个必须实现
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view;
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view;
@optional
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view;
实现代理:
//触发刷新
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView *)view{
//这里一般是网络请求
_isRefresh = YES;
//刷新操作,此处用一个定时器来模拟网络请求
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(refresh) userInfo:nil repeats:NO];
}
//告诉refreshView现在是否正在刷新
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView *)view{
return _isRefresh;}
7.模拟刷新之后
//下载完成
- (void)refresh{
[_dataArray addObject:@"new"];
[_tableView reloadData];
//结束刷新
[_refreshView egoRefreshScrollViewDataSourceDidFinishedLoading:_tableView];
//将刷新标记为NO,防止多次向下拖拽重复请求
_isRefresh = NO;
}