以前看到facebook,人人和其他的一些应用,很多都使用了下拉列表,列表就重新加载数据的应用.感觉用户体验很好,但是一直没有时间找相应的解决方法,今天终于有时间整理一下代码:
下图是实现的效果图,当用户下拉列表的时候,就出现向上的箭头.
实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh
实现过程很简单,下载相应的demo,然后拷贝资源文件和EGORefreshTableHeaderView.h和EGORefreshTableHeaderView.m到相依的工程中.在table所在的类中添加如下代码:
- #import <UIKit/UIKit.h>
- #import "EGORefreshTableHeaderView.h"
- @interface iphone_navtaViewController : UIViewController
- <UITableViewDelegate,UITableViewDataSource,EGORefreshTableHeaderDelegate>{
- BOOL isflage;
- IBOutlet UITableView *myTableView;
- EGORefreshTableHeaderView *_refreshHeaderView;
- BOOL _reloading;
- }
- @property (nonatomic,retain) UITableView *myTableView;
- - (void)reloadTableViewDataSource;
- - (void)doneLoadingTableViewData;
- @end
实现方法:
- #import "iphone_navtaViewController.h"
- @implementation iphone_navtaViewController
- @synthesize myTableView;
- - (void)viewDidLoad {
- [super viewDidLoad];
- if (_refreshHeaderView == nil) {
- EGORefreshTableHeaderView *view1 = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 10.0f – self.myTableView.bounds.size.height, self.myTableView.frame.size.width, self.view.bounds.size.height)];
- view1.delegate = self;
- [self.myTableView addSubview:view1];
- _refreshHeaderView = view1;
- [view1 release];
- }
- [_refreshHeaderView refreshLastUpdatedDate];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- self.myTableView=nil;
- _refreshHeaderView=nil;
- }
- - (void)dealloc {
- _refreshHeaderView=nil;
- [self.myTableView release];
- [super dealloc];
- }
- #pragma mark –
- #pragma mark onClick
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- isflage=!isflage;
- [super.navigationController setNavigationBarHidden:isflage animated:TRUE];
- [super.navigationController setToolbarHidden:isflage animated:TRUE];
- }
- #pragma mark –
- #pragma mark UITableView
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return 3;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
- if (cell==nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
- reuseIdentifier:@"tag"] autorelease];
- }
- //表格设计
- return cell;
- }
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 100;
- }
- #pragma mark –
- #pragma mark Data Source Loading / Reloading Methods
- - (void)reloadTableViewDataSource{
- NSLog(@"==开始加载数据");
- _reloading = YES;
- }
- - (void)doneLoadingTableViewData{
- NSLog(@"===加载完数据");
- _reloading = NO;
- [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.myTableView];
- }
- #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;
- }
- - (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
- return [NSDate date];
- }
- @end
一个最重要的步骤,就是要添加QuartzCore.framework组建。
然后运行即可。