本文用到的是:RefreshTable.
文件下载请移步:https://github.com/enormego/EGOTableViewPullRefresh
本来想传图的。可不知道怎么回事,无法上传。暂时不上传了
1.请将RefreshTableHeaderView的两个相关文件及图片文件 引入你的工程中
2.引入相关
.h中的操作
2.1 #import "EGORefreshTableHeaderView.h"
2.2 添加
EGORefreshTableHeaderDelegate ,还要记得引入UIScrollViewDelegate
2.3 在.h中添加
EGORefreshTableHeaderView *_refreshHeaderView;
BOOL _reloading;
2.4 添加两个方法
- (void)reloadTableViewDataSource;
- (void)doneLoadingTableViewData;
如下:
#import <UIKit/UIKit.h> #import "EGORefreshTableHeaderView.h" @interface KeHuanWangFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,EGORefreshTableHeaderDelegate>{ EGORefreshTableHeaderView *_refreshHeaderView; BOOL _reloading;//主要是记录是否在刷新中 } //这两个是下拉刷新的事件 - (void)reloadTableViewDataSource; - (void)doneLoadingTableViewData;
//下拉刷新相关代码--------------- //重加载事件 - (void) reloadTableViewDataSource{ _reloading = YES; //这里引用你加载数据的方法 } //加载结束事件 - (void)doneLoadingTableViewData{ // model should call this when its done loading _reloading = NO; if (得到的数组数是大于0的) { [self.table reloadData]; }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"获取数据失败或网络异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [alert release]; } [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.table]; }#pragma mark -#pragma mark UIScrollViewDelegate Methods //table也是scrollview所以只要滚动就会调用这个方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"Scrolling..."); [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView]; } //滚动结束就会调用这个方法 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"Scrolling Stoped..."); [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView]; } #pragma mark - #pragma mark EGORefreshTableHeaderDelegate Methods //释放更新 - (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{ // // [self reloadTableViewDataSource]; [NSThread detachNewThreadSelector:@selector(reloadTableViewDataSource) toTarget:self withObject:nil]; // [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0]; } - (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{ return _reloading; // should return if data source model is reloading } //最后一次改变的数据 - (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{ return [NSDate date]; // should return date data source was last changed } #pragma mark - View lifecycle //手动调用下拉刷新的代码 -------------- -(void) ViewFrashData { [self.tabNews setContentOffset:CGPointMake(0, -75) animated:YES]; [self performSelector:@selector(doneManualRefresh) withObject:nil afterDelay:0.4]; } -(void)doneManualRefresh { [_refreshHeaderView egoRefreshScrollViewDidScroll:self.tabNews]; [_refreshHeaderView egoRefreshScrollViewDidEndDragging:self.tabNews]; } //下拉刷新相关代码--------------- //手动调用下拉刷新的代码 -------------- //刷新按钮事件 - (void) reloadInfo { //手动下拉刷新相关 [self ViewFrashData]; } - (void)viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //下拉刷新相关 if ( _refreshHeaderView ==nil) { EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f,0.0f - self.tabNews.bounds.size.height,self.view.frame.size.width,self.tabNews.bounds.size.height)]; //0,-416,320,416 NSLog(@"%f",self.tabNews.bounds.size.height); view.delegate = self; [self.tabNewsaddSubview:view]; _refreshHeaderView = view; [view release]; } // update the last update date [_refreshHeaderViewrefreshLastUpdatedDate]; //如果需要进入view或按刷新按钮就出现下拉加载效果。可以直接调用reloadInfo方法