首页 > 代码库 > IOS怎么实现一个UITableView的下拉刷新

IOS怎么实现一个UITableView的下拉刷新

采用的EGORefreshTableHeaderView来实现:

在Controller上实现EGORefreshTableHeaderDelegate的delegate

@property(nonatomic)UITableView* tableView;
@property(nonatomic)NSMutableArray* data;
@property(nonatomic)EGORefreshTableHeaderView* refreshTableVIew;
@property(nonatomic,assign)BOOL reloading;
先初始化:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    UIToolbar* bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
   
    [bar setItems:[NSArray arrayWithObjects:item, nil]];
    [self.view addSubview:bar];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44,320,460-44)];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _data = http://www.mamicode.com/[[NSMutableArray alloc] init];>


下拉的时候会执行

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    _reloading = YES;
    //开始刷新后执行后台线程,在此之前可以开启HUD或其他对UI进行阻塞
    [NSThread detachNewThreadSelector:@selector(doInBackground) toTarget:self withObject:nil];
}


-(void)doInBackground{
    NSLog(@"do in background");
    [_data addObject:@"ff"];
    [NSThread sleepForTimeInterval:3];
    [self performSelectorOnMainThread:@selector(loadedTableViewData) withObject:nil waitUntilDone:YES];
}


-(void)loadedTableViewData{
    _reloading = NO;
    [_refreshTableVIew egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    [self.tableView reloadData];
}

这个方法表示正在刷新中

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    return _reloading;
}



这个方法在显示时间在TableView的刷新块上 :

//返回刷新时间的回调方法
-(NSDate *)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView *)view
{
    return [NSDate date];
}

然后必须加上下面两个方法:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_refreshTableVIew egoRefreshScrollViewDidScroll:scrollView];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_refreshTableVIew egoRefreshScrollViewDidEndDragging:scrollView];
}


代码可以在http://download.csdn.net/detail/baidu_nod/7599913下载