//加载更多
_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
_moreButton.backgroundColor = [UIColor clearColor];
_moreButton.frame = CGRectMake(0, 0, kScreenWidth, 40);
_moreButton.titleLabel.font = [UIFont systemFontOfSize:15];
[_moreButton setTitle:@"上拉加载更多..." forState:UIControlStateNormal];
[_moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_moreButton addTarget:self action:@selector(loadMoreAction) forControlEvents:UIControlEventTouchUpInside];
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.frame = CGRectMake(100, 10, 20, 20);
activityView.tag = 2000;
[activityView stopAnimating];
[_moreButton addSubview:activityView];
[activityView release];
self.tableFooterView = _moreButton;
//加载更多
-(void)loadMoreAction
{
//如果没有更多,就返回
if (!self.isMore) {
return;
}
if (_pullUpBlock) {
//刷新正在加载的状态
[self startLoadMore];
//执行上拉的操作
_pullUpBlock(self);
}
}
-(void)startLoadMore
{
[_moreButton setTitle:@"正在加载.." forState:UIControlStateNormal];
_moreButton.enabled = NO;
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[_moreButton viewWithTag:2000];
[activityView startAnimating];
}
-(void)stopLoadMore
{
if (self.data.count > 0) {
_moreButton.hidden = NO;
_moreButton.enabled = YES;
[_moreButton setTitle:@"上拉加载更多..." forState:UIControlStateNormal];
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[_moreButton viewWithTag:2000];
[activityView stopAnimating];
//加载完成,如果没有更多的数据,则也隐藏
if (!self.isMore) {
_moreButton.hidden = YES;
}
}else{
_moreButton.hidden = YES;
}
}
//重写reloadData方法
-(void)reloadData
{
[super reloadData];
//停止加载更多
[self stopLoadMore];
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
//-------- 上拉加载更多 -------
//实现原理: contentOffset.height + height = contentSize.height
//y坐标滚动的偏移量
float offset = scrollView.contentOffset.y;
//scrollView的内容高度
float contentHeight = scrollView.contentSize.height;
//当offset偏移量滑到底部时,差值是scrollView的高度
float sub = contentHeight-offset;
// float sub = scrollView.contentSize.height-scrollView.contentOffset.y;
if (scrollView.height - sub > 50) {
[self loadMoreAction];
}
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
//已经触发了下拉刷新
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
//刷新数据
[self reloadTableViewDataSource];
//刷新数据
if (_pullDownBlock) {
_pullDownBlock(self);
}
// //模拟 停止加载,弹回下拉
// [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:1.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
}