自定義下拉刷新與上拉加載控件

1.headerView不要用tableView的tableHeaderView頭視圖 , 因為一般的頭視圖會用做廣告條使用 ;
2.調(diào)整UIEdgeInsets屬性來設置headerView上的headerLabel的變化 , 做到下拉與回退的效果 ;
3.本示例代碼中的數(shù)據(jù)都是mock的假數(shù)據(jù) ;
4.設置一個BOOL屬性記錄是否正在刷新,正在加載的判斷 , 如果正在刷新或者加載的狀態(tài)我就return , 防止誤操作 ;
5.數(shù)據(jù)請求成功后要回退到原來的位置 , 數(shù)據(jù)也要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 ;
    //滾動條的內(nèi)邊距與tableView的內(nèi)邊距相同:
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset ;
    //tabBarButton按鈕監(jiān)聽:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarButtonDidRepeatClick) name:BSTabBarButtonDidRepeatClickNotification object:nil] ;
    //titleButton按鈕監(jiān)聽:
    [[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 = @"下拉加載更多數(shù)據(jù)..." ;
    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(@"%@ -- 刷新數(shù)據(jù)" , 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時調(diào)用該方法(停止拖拽方法):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.isHeaderRefreshing) return ;
    //可以通過文字來判斷:
    //可以通過背景色:
    //可以通過偏移量來判斷:
    //感覺偏移量更正規(guī)的說:
    CGFloat offsetY = - (64 + 35 + 50) ;
    if (self.tableView.contentOffset.y <= offsetY){
        //進入下拉刷新狀態(tài) :
        self.headerRefreshing = YES ;
        self.headerLabel.text = @"正在加載新數(shù)據(jù)..." ;
        self.headerLabel.textColor = [UIColor redColor] ;
        self.headerLabel.backgroundColor = [UIColor blueColor] ;
        //增大內(nèi)邊距:
        [UIView animateWithDuration:0.25 animations:^{
            UIEdgeInsets inset = self.tableView.contentInset  ;
            inset.top += self.headerView.height ;
            self.tableView.contentInset = inset ;
        }] ;

        //模擬數(shù)據(jù)返回成功:
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //服務器的數(shù)據(jù)回來了!
            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 = @"下拉加載更多數(shù)據(jù)..." ;
                self.headerLabel.textAlignment = NSTextAlignmentCenter ;
                //此時還要把當前控件的是否正在刷新狀態(tài)改為NO:
                self.headerRefreshing = NO ;
            });
        });
    }
}


#pragma mark - 處理header
- (void)handleHeader {
    //如果正在下拉刷新直接返回:
    //防止在處理下拉加載數(shù)據(jù)的時候再次進入下面的代碼內(nèi)容:
    if (self.isHeaderRefreshing) return ;
    //當scrollView的偏移量小于等于offsetY的時候 , 說明下拉刷新控件完全出現(xiàn)了!
    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 = @"下拉加載更多數(shù)據(jù)..." ;
        self.headerLabel.textAlignment = NSTextAlignmentCenter ;
    }
}


#pragma mark - 處理footer
- (void)handleFooter {
    //如果內(nèi)容還沒有出現(xiàn) , 還是0值 , 那么就return ;否則會在一開始就顯示"footerView完全出現(xiàn)"的情況 ;
    if (self.tableView.contentSize.height == 0) return ;
    CGFloat offsetY = self.tableView.contentSize.height + self.tableView.contentInset.bottom - self.tableView.height ;
    //如果正在刷新 , 說明正在發(fā)請求給服務器 , 就沒必要再次進入這個重復發(fā)送請求的狀態(tài)了!
    if (self.isFooterRefreshing) return ;
    if (self.tableView.contentOffset.y >=offsetY) {
        BSLog(@"footerView完全出現(xiàn)") ;
        self.footerRefreshing = YES ;
        //進入刷新狀態(tài):
        self.footerLabel.text = @"正在加載更多數(shù)據(jù)..." ;
        self.footerLabel.backgroundColor = [UIColor blueColor] ;
        BSLog(@"發(fā)送請求給服務器...") ;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 2秒后服務器返回數(shù)據(jù)成功:
            self.dataCount += 5 ;
            [self.tableView reloadData] ;
            // 1.結束刷新:
            self.footerRefreshing =NO ;
            //文字 , 顏色等細部UI都要改回去:
            self.footerLabel.backgroundColor = [UIColor redColor] ;
            self.footerLabel.text = @"上拉加載更多" ;
        });
    }
}


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

@end

愿編程讓這個世界更美好

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 翻譯自“Collection View Programming Guide for iOS” 0 關于iOS集合視...
    lakerszhy閱讀 4,071評論 1 22
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • 一.上拉刷新 1.為什么要做上拉刷新? 想要看一些舊的(更多)數(shù)據(jù),就需要上拉刷新,加載更多數(shù)據(jù) 2.上拉刷新永遠...
    尕小天閱讀 1,035評論 0 5
  • 如果在一個不經(jīng)意的抬頭,看到了心心念念卻好久不見的人,你會是怎樣的心情? 忽然就是在一個不經(jīng)意的抬頭再次...
    羿十一閱讀 1,558評論 24 15
  • 單位舉辦迎七一詩歌朗誦比賽,我踴躍報名了。和同事們一起參加四個人的集體朗誦。我們朗誦的內(nèi)容是著名的青年作家高原紅老...
    自在心靈空間閱讀 630評論 0 2

友情鏈接更多精彩內(nèi)容