自定义下拉刷新与上拉加载控件

1.headerView不要用tableView的tableHeaderView头视图 , 因为一般的头视图会用做广告条使用 ;
2.调整UIEdgeInsets属性来设置headerView上的headerLabel的变化 , 做到下拉与回退的效果 ;
3.本示例代码中的数据都是mock的假数据 ;
4.设置一个BOOL属性记录是否正在刷新,正在加载的判断 , 如果正在刷新或者加载的状态我就return , 防止误操作 ;
5.数据请求成功后要回退到原来的位置 , 数据也要reloadData , 请求也要结束 ;

#import "TestViewController.h"

@interface AllViewController ()
@property (nonatomic , strong) UIView *footerView ;
@property (nonatomic , strong) UILabel *footerLabel ;
@property (nonatomic , strong) UIView *headerView ;
@property (nonatomic , strong) UILabel *headerLabel ;
//记录上拉加载控件是否正在刷新
@property (nonatomic , assign , getter=isFooterRefreshing) BOOL footerRefreshing ;
//记录下拉刷新控件是否正在刷新
@property (nonatomic , assign , getter=isHeaderRefreshing) BOOL headerRefreshing ;
@property (nonatomic , assign) NSInteger dataCount ;

@end

@implementation AllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataCount = 30 ;
    self.view.backgroundColor = BSRandomColor ;
    self.tableView.contentInset = ContentInset ;
    //滚动条的内边距与tableView的内边距相同:
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset ;
    //tabBarButton按钮监听:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarButtonDidRepeatClick) name:BSTabBarButtonDidRepeatClickNotification object:nil] ;
    //titleButton按钮监听:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(titleButtonDidRepeatClick) name:BSTitleButtonDidRepeatClickNotification object:nil] ;
    //上拉加载控件:
    [self setupRefresh] ;
    
}

#pragma mark - 自定义上拉加载控件
- (void)setupRefresh {
    
    //广告条:
    UILabel *ADLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 30)] ;
    ADLabel.text = @"假装有广告" ;
    ADLabel.textColor = [UIColor blackColor] ;
    ADLabel.backgroundColor = [UIColor whiteColor] ;
    ADLabel.textAlignment = NSTextAlignmentCenter ;
    self.tableView.tableHeaderView = ADLabel ;
    
    //header:
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -50, self.tableView.width, 50)] ;
    headerView.backgroundColor = [UIColor redColor] ;
    self.headerView = headerView ;
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerView.bounds] ;
    headerLabel.backgroundColor = [UIColor blackColor] ;
    headerLabel.textColor = [UIColor redColor] ;
    headerLabel.text = @"下拉加载更多数据..." ;
    headerLabel.textAlignment = NSTextAlignmentCenter ;
    self.headerLabel = headerLabel ;
    [headerView addSubview:headerLabel] ;
    [self.tableView addSubview:headerView] ;
    
    //footer:
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width, 35)] ;
    self.footerView = footerView ;
    UILabel *footerLabel = [[UILabel alloc] init] ;
    footerLabel.frame = footerView.bounds ;
    footerLabel.backgroundColor = [UIColor redColor] ;
    footerLabel.text = @"上拉加载更多" ;
    footerLabel.textAlignment = NSTextAlignmentCenter ;
    self.footerLabel = footerLabel ;
    [footerView addSubview:footerLabel] ;
    self.tableView.tableFooterView = footerView ;
}


#pragma mark - tabBarButton按钮被重复点击了
- (void)tabBarButtonDidRepeatClick {
    //如果当前控制器不在主window上 , 说明点击的不是精华按钮:
    if (self.view.window == nil) return ;
    //如果是精华按钮,但是还需要判断是不是全部模块:
    if (self.tableView.scrollsToTop == NO) return ;
    BSLog(@"%@ -- 刷新数据" , self.class) ;
}


#pragma mark - titleButton按钮被重复点击了
- (void)titleButtonDidRepeatClick {
    //与tabBarButtonDidRepeatClick的方法相同:
    [self tabBarButtonDidRepeatClick] ;
}


#pragma mark - 移除所有通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self] ;
}


#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.footerView.hidden = (self.dataCount == 0) ;
    return self.dataCount ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *const AllCell = @"AllCell" ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AllCell] ;
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AllCell] ;
        cell.backgroundColor = [UIColor clearColor] ;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"test --%@-- %zd" ,[self class] , indexPath.row] ;
    return cell ;
}


#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self handleHeader] ;
    [self handleFooter] ;
}

//用户松开scrollView时调用该方法(停止拖拽方法):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.isHeaderRefreshing) return ;
    //可以通过文字来判断:
    //可以通过背景色:
    //可以通过偏移量来判断:
    //感觉偏移量更正规的说:
    CGFloat offsetY = - (64 + 35 + 50) ;
    if (self.tableView.contentOffset.y <= offsetY){
        //进入下拉刷新状态 :
        self.headerRefreshing = YES ;
        self.headerLabel.text = @"正在加载新数据..." ;
        self.headerLabel.textColor = [UIColor redColor] ;
        self.headerLabel.backgroundColor = [UIColor blueColor] ;
        //增大内边距:
        [UIView animateWithDuration:0.25 animations:^{
            UIEdgeInsets inset = self.tableView.contentInset  ;
            inset.top += self.headerView.height ;
            self.tableView.contentInset = inset ;
        }] ;

        //模拟数据返回成功:
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //服务器的数据回来了!
            self.dataCount = 20 ;
            [self.tableView reloadData] ;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //2s后结束刷新:
                [UIView animateWithDuration:0.25 animations:^{
                    UIEdgeInsets inset = self.tableView.contentInset ;
                    inset.top -= self.headerView.height ;
                    self.tableView.contentInset = inset ;
                }] ;
                //文字和背景色等都要还原:
                self.headerLabel.backgroundColor = [UIColor blackColor] ;
                self.headerLabel.textColor = [UIColor redColor] ;
                self.headerLabel.text = @"下拉加载更多数据..." ;
                self.headerLabel.textAlignment = NSTextAlignmentCenter ;
                //此时还要把当前控件的是否正在刷新状态改为NO:
                self.headerRefreshing = NO ;
            });
        });
    }
}


#pragma mark - 处理header
- (void)handleHeader {
    //如果正在下拉刷新直接返回:
    //防止在处理下拉加载数据的时候再次进入下面的代码内容:
    if (self.isHeaderRefreshing) return ;
    //当scrollView的偏移量小于等于offsetY的时候 , 说明下拉刷新控件完全出现了!
    CGFloat offsetY = - (64 + 35 + 50) ;
    if (self.tableView.contentOffset.y <= offsetY) {
        self.headerLabel.backgroundColor = [UIColor yellowColor] ;
        self.headerLabel.textColor = [UIColor blueColor] ;
        self.headerLabel.text = @"松开立即刷新..." ;
        self.headerLabel.textAlignment = NSTextAlignmentCenter ;
    } else {
        //退回去之后还要返回原来的颜色:
        self.headerLabel.backgroundColor = [UIColor blackColor] ;
        self.headerLabel.textColor = [UIColor redColor] ;
        self.headerLabel.text = @"下拉加载更多数据..." ;
        self.headerLabel.textAlignment = NSTextAlignmentCenter ;
    }
}


#pragma mark - 处理footer
- (void)handleFooter {
    //如果内容还没有出现 , 还是0值 , 那么就return ;否则会在一开始就显示"footerView完全出现"的情况 ;
    if (self.tableView.contentSize.height == 0) return ;
    CGFloat offsetY = self.tableView.contentSize.height + self.tableView.contentInset.bottom - self.tableView.height ;
    //如果正在刷新 , 说明正在发请求给服务器 , 就没必要再次进入这个重复发送请求的状态了!
    if (self.isFooterRefreshing) return ;
    if (self.tableView.contentOffset.y >=offsetY) {
        BSLog(@"footerView完全出现") ;
        self.footerRefreshing = YES ;
        //进入刷新状态:
        self.footerLabel.text = @"正在加载更多数据..." ;
        self.footerLabel.backgroundColor = [UIColor blueColor] ;
        BSLog(@"发送请求给服务器...") ;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 2秒后服务器返回数据成功:
            self.dataCount += 5 ;
            [self.tableView reloadData] ;
            // 1.结束刷新:
            self.footerRefreshing =NO ;
            //文字 , 颜色等细部UI都要改回去:
            self.footerLabel.backgroundColor = [UIColor redColor] ;
            self.footerLabel.text = @"上拉加载更多" ;
        });
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

愿编程让这个世界更美好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,931评论 1 22
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • 一.上拉刷新 1.为什么要做上拉刷新? 想要看一些旧的(更多)数据,就需要上拉刷新,加载更多数据 2.上拉刷新永远...
    尕小天阅读 997评论 0 5
  • 如果在一个不经意的抬头,看到了心心念念却好久不见的人,你会是怎样的心情? 忽然就是在一个不经意的抬头再次...
    羿十一阅读 1,495评论 24 15
  • 单位举办迎七一诗歌朗诵比赛,我踊跃报名了。和同事们一起参加四个人的集体朗诵。我们朗诵的内容是著名的青年作家高原红老...
    自在心灵空间阅读 525评论 0 2