openSource:https://github.com/enormego/EGOTableViewPullRefresh 里面有demo
1、下载EGORefreshTableHeaderView开源框架。
2、往应用中添加EGORefreshTableHeaderView.h文件、EGORefreshTableHeaderView.m文件和Resource目录,并且导入QuartzCore.framework
3、添加代码
在tableview对应的类的h文件中添加如下代码
#import "EGORefreshTableHeaderView.h"
添加接口 EGORefreshTableHeaderDelegate
@property (strong, nonatomic) EGORefreshTableHeaderView *refreshHeaderView;
@property (assign, nonatomic)BOOL reloading;
- (void)reloadTableViewDataSource;
- (void)doneLoadingTableViewData;
在m文件中:
往viewDidLoad函数里添加如下代码(自己微调)
if (refreshHeaderView == nil)
{
// 创建下拉视图
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f,0.0f - myTableView.bounds.size.height, self.view.frame.size.width, myTableView.bounds.size.height)];
view.delegate = self;
[myTableView addSubview:view];
refreshHeaderView = view;
}
// 更新时间
[refreshHeaderView refreshLastUpdatedDate];
再添加一些事件函数
#pragma mark -
#pragma mark Data Source Loading / Reloading Methods
// 刷新开始时调用
- (void)reloadTableViewDataSource
{
// should be calling your tableviews data source model to reload
// put here just for demo
/*
在这添加自己代码
[self resetSearch];
*/
reloading = YES;
}
// 刷新结束时调用
- (void)doneLoadingTableViewData
{
// model should call this when its done loading
/*
在这添加自己代码
search.text = @"";
[myTableView reloadData];
[search resignFirstResponder];
reloading = NO;
*/
[refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:myTableView];
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
// 页面滚动时回调
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//NSLog(@"scrollViewDidScroll");
[refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
// 滚动结束时回调
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//NSLog(@"scrollViewDidEndDragging");
[refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
// 开始刷新时回调
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
NSLog(@"egoRefreshTableHeaderDidTriggerRefresh");
[self reloadTableViewDataSource];
[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:1.0];
}
// 下拉时回调
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
NSLog(@"egoRefreshTableHeaderDataSourceIsLoading");
return reloading; // should return if data source model is reloading
}
// 请求上次更新时间时调用
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
NSLog(@"egoRefreshTableHeaderDataSourceLastUpdated");
return [NSDate date]; // should return date data source was last changed
}
最后更具自己需求修改源代码的,如样式,时间格式等
BTW:下面2个地址用到这个代码
http://justsee.iteye.com/blog/1629364
这里有基于这个代码的下拉刷新 http://blog.csdn.net/linzhu0831/article/details/7547337