• UITableView 上拉刷新控件


    .h 文件

    #import <UIKit/UIKit.h>
    
    @protocol RefreshTableFooterViewDelegate;
    @interface RefreshTableFooterView : UIView
    @property (nonatomic, weak) id<RefreshTableFooterViewDelegate> delegate;
    
    // 实现ScrollViewDelegate 后在对应的 didEndingDragging 方法中调用
    -(void)refreshFooterScrollViewDidEndDragging:(UIScrollView *)scrollView;
    
    // 在重新刷新 tableView 后调用
    -(void)refreshFooterViewDataSourceDidFinishedLoading:(UIScrollView*)scrollView;
    
    // 在重新刷新 tableView 之前调用
    -(void)refreshFooterViewShouldBeHiddenBeforeDataSourceReloadNewData;
    @end
    
    @protocol RefreshTableFooterViewDelegate <NSObject>
    
    -(void)refreshTableFooterViewTriggerRefresh;
    -(BOOL)refreshTableFooterViewIsLoading;
    -(BOOL)refreshTableFooterViewHasMore;
    
    @end
    
    

    .m 文件

    #import "RefreshTableFooterView.h"
    
    @implementation RefreshTableFooterView
    {
        UIActivityIndicatorView *_indicatorView;
        UILabel *_descriptionLabel;
        BOOL _isLastPage;
        BOOL _hasRefreshed;
    }
    
    -(instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            CGFloat yAxis = 3;
            CGFloat width = 200;
            
            _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            _indicatorView.frame = CGRectMake((CGRectGetWidth(self.bounds) - 200)/2 - CGRectGetWidth(_indicatorView.bounds), yAxis, CGRectGetWidth(_indicatorView.bounds), CGRectGetHeight(_indicatorView.bounds));
            _indicatorView.hidesWhenStopped = YES;
            [self addSubview:_indicatorView];
            [_indicatorView stopAnimating];
            
            _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.bounds) - 200)/2, yAxis, width, CGRectGetHeight(_indicatorView.bounds))];
            _descriptionLabel.text = @"上拉刷新";
            _descriptionLabel.textAlignment = NSTextAlignmentCenter;
            _descriptionLabel.textColor = [UIColor lightGrayColor];
            [self addSubview:_descriptionLabel];
        }
        return self;
    }
    
    -(void)refreshFooterScrollViewDidEndDragging:(UIScrollView *)scrollView
    {
        if (!self.delegate ||
            ![self.delegate respondsToSelector:@selector(refreshTableFooterViewIsLoading)] ||
            [self.delegate refreshTableFooterViewIsLoading]) {
            return;
        }
        
        if (![self.delegate refreshTableFooterViewHasMore]) {
            _descriptionLabel.text = @"已经加载所有~";
            return;
        }
        
        CGFloat contentHeight = scrollView.contentSize.height;
        CGFloat offsetY = scrollView.contentOffset.y;
        CGFloat containerHeight = scrollView.bounds.size.height;
        BOOL refreshOrNot = containerHeight + offsetY > contentHeight - 0;
        if (refreshOrNot && [self.delegate respondsToSelector:@selector(refreshTableFooterViewTriggerRefresh)]) {
            [self.delegate refreshTableFooterViewTriggerRefresh];
            _descriptionLabel.text = @"加载中...";
            _indicatorView.hidden = NO;
            [_indicatorView startAnimating];
            [scrollView setContentInset:UIEdgeInsetsMake(.0, 50.0, .0, .0)];
        }
    }
    
    -(void)refreshFooterViewShouldBeHiddenBeforeDataSourceReloadNewData {
        self.hidden = YES;
    }
    
    -(void) refreshFooterViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
        [scrollView setContentInset:UIEdgeInsetsMake(.0, .0, .0, .0)];
        CGRect original = self.frame;
        original.origin.y = scrollView.contentSize.height > scrollView.bounds.size.height ? scrollView.contentSize.height : scrollView.bounds.size.height;
        self.frame = original;
        self.hidden = NO;
        [_indicatorView stopAnimating];
        _descriptionLabel.text = @"上拉刷新";
    }
    
    
    @end
    
    
    
  • 相关阅读:
    ssh 私匙登录, 文件rswrst权限
    从内存使用的角度来理解.Net底层架构
    (转)C#为什么要使用Invoke,它和BeginInvoke有什么区别
    如何通过微信自定义菜单跳转到自己的网站
    (转)HubbleDotNet+Mongodb 构建高性能搜索引擎--概述
    (转)HubbleDotNet 和 Lucene.net 性能对比测试
    C#异步提示和技巧
    关于System.Windows.Forms.DateTimePicker的一个Bug
    关于frameset中指定区域回退的实现
    java.lang.NoClassDefFoundError Adding a jar to an RCP application
  • 原文地址:https://www.cnblogs.com/1oo1/p/4383949.html
Copyright © 2020-2023  润新知