自定义的上拉加载更多

 
    //加载更多
    _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
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值