一、找到你的 UITableView / UITableViewController 的 .h 文件,结合以下代码,添加相应的元素:
#import "EGORefreshTableHeaderView.h"
@interface RootViewController : UITableViewController {
EGORefreshTableHeaderView *_refreshHeaderView;
// Reloading var should really be your tableviews datasource
// Putting it here for demo purposes
BOOL _reloading;
}
-(void)reloadTableViewDataSource;
-(void)doneLoadingTableViewData;
@end二、切换到你的 UITableView / UITableViewController 的 .m 文件,结合以下代码,添加相应的元素: -(void)viewDidLoad {
[super viewDidLoad];
if(_refreshHeaderView ==nil){
EGORefreshTableHeaderView *view =[[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
[view release];
}
// update the last update date
[_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
_reloading =YES;
}
-(void)doneLoadingTableViewData{
// model should call this when its done loading
_reloading =NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
-(void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
[self reloadTableViewDataSource];
[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
}三、最后呢,为了彰显你良好的内存管理习惯,别忘了释放掉相应的 UI 元素:
-(void)viewDidUnload {
_refreshHeaderView=nil;
}
-(void)dealloc {
_refreshHeaderView =nil;
[super dealloc];
}