2018-05-28

对MJRefresh.h再次封装,上拉加载更多,下拉刷新

  • 在公司称着没事的时间随便写写,自己对MJRefresh再次做了下简单封装,可以满足一般的需求。废话不多说,直接上代码。

  • 首先是.h文件

     #import 
     @protocol WCMJRefreshDelegate
     //下拉刷新
    -(void)loadNewTopicWithPageCurrent:(NSInteger)page;
     //上拉加载更多
    -(void)loadMoreTopicWithPageCurrent:(NSInteger)page;
    @end
    
    @interface WCMJRefresh : NSObject
    //下拉刷新闲置状态标题
    @property (nonatomic,copy) NSString *loadNewIdleTitle;
    //下拉刷新正在加载时的标题
    @property (nonatomic,copy) NSString *loadNewRefreshingTitle;
    //上拉加载更多闲置时标题
    @property (nonatomic,copy) NSString *loadMoreIdleTitle;
    //上拉加载更多正在刷新时标题
    @property (nonatomic,copy) NSString *loadMoreRefreshingTitle;
    
    @property (nonatomic,strong) UITableView *tableView;
    @property (nonatomic,weak) id delegate;
    //下拉刷新结束
    - (void)mj_headerEndRefreshing;
    //上拉加载更多结束
    - (void)mj_footerEndRefreshing;
    //数据加载完毕
    - (void)loadFinish;
    //重置当前页
    - (void)ResetCurrentPage;
    @end
    
  • .m文件具体实现

    #import "WCMJRefresh.h"
    #import "MJRefresh.h"
    @interface WCMJRefresh()
    @property (nonatomic,strong) MJRefreshGifHeader *header;
    @property (nonatomic,strong) MJRefreshBackNormalFooter *footer;
    //当前页
    @property (nonatomic,assign) NSInteger pageCurr;
    @end
    @implementation WCMJRefresh
    
     - (MJRefreshGifHeader *)header
    {
    if (!_header) {
      _header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopic)];
      _header.automaticallyChangeAlpha = YES;
      }
      return _header;
     }
    
    - (MJRefreshBackNormalFooter *)footer
    {
      if (!_footer) {
      _footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
       }
       return _footer;
    }
    //重写初始化方法
    - (instancetype)init
    {
      self = [super init];
      if (self) {
      //初始化页码
      _pageCurr = 1;
      
      }
      return self;
    }
    - (void)setTableView:(UITableView *)tableView
    {
     _tableView = tableView;
    
     //下拉刷新
    self.tableView.mj_header = self.header;
    //上拉加载更多
    self.tableView.mj_footer = self.footer;
    }
    //加载新的数据
    - (void)loadNewTopic
    {
    //更新页码
    _pageCurr = 1;
    
     if ([self.delegate respondsToSelector:@selector(loadNewTopicWithPageCurrent:)]) {
      [self.delegate loadNewTopicWithPageCurrent:_pageCurr];
      }
     //置上拉加载更多为闲置状态
     self.footer.state = MJRefreshStateIdle;
    
    }
    //加载更多
    - (void)loadMoreTopic
    {
    _pageCurr ++;
    
     if (self.footer.state != MJRefreshStateIdle) {
      
      if ([self.delegate respondsToSelector:@selector(loadMoreTopicWithPageCurrent:)]) {
          [self.delegate loadMoreTopicWithPageCurrent:_pageCurr];
      }
      }
    
    
      }
    
     //刷新时标题    下拉刷新
     - (void)setLoadNewIdleTitle:(NSString *)loadNewIdleTitle
    {
        _loadNewIdleTitle = loadNewIdleTitle;
        [_header setTitle:self.loadNewIdleTitle forState:MJRefreshStateIdle];
    
    }
    - (void)setLoadNewRefreshingTitle:(NSString *)loadNewRefreshingTitle
    {
        _loadNewRefreshingTitle = loadNewRefreshingTitle;
        [_header setTitle:self.loadNewRefreshingTitle forState:MJRefreshStateRefreshing];
        }
       //上拉加载更多
       - (void)setLoadMoreIdleTitle:(NSString *)loadMoreIdleTitle
       {
           _loadMoreIdleTitle = loadMoreIdleTitle;
           [_footer setTitle:self.loadMoreIdleTitle forState:MJRefreshStateIdle];
    
       }
       -(void)setLoadMoreRefreshingTitle:(NSString *)loadMoreRefreshingTitle
       {
           _loadMoreRefreshingTitle = loadMoreRefreshingTitle;
           [_footer setTitle:self.loadMoreRefreshingTitle forState:MJRefreshStateRefreshing];
       }
    
      //结束头部刷新
      - (void)mj_headerEndRefreshing
      {
          [self.tableView.mj_header endRefreshing];
      }
      //结束尾部刷新
      - (void)mj_footerEndRefreshing
      {
           [self.tableView.mj_footer endRefreshing];
      }
     //数据加载完毕
     - (void)loadFinish
     {
          self.footer.state = MJRefreshStateNoMoreData;
     }
     //重置当前页
     - (void)ResetCurrentPage
    {
         self.pageCurr = 1;
    }
    @end
    

你可能感兴趣的:(2018-05-28)