mjrefresh自动判断是否隐藏mj_footer

近期项目用到了mjrefresh,产品要求列表的数据不满一页的话就自动隐藏下面的“上拉加载更多”或是“到底了”,写了个UItableview的分类来实现了这个功能,主要原理就是:数据加载完成时如果tableview的footer也就是mj_footer在屏幕显示区域,则说明列表数据不满一页,将footer的文字@“”,从而达到隐藏的目的。

代码如下:

// MJRefresh+FooterManger.h

#import

#import "MJRefresh.h"


typedef NS_ENUM(NSInteger, MJFooterRefreshState) {

    MJFooterRefreshStateNormal,

    MJFooterRefreshStateLoadMore,

    MJFooterRefreshStateNoMore

};


@interface UITableView (MJRefreshAutoManger)

@property (nonatomic,assign)MJFooterRefreshState footRefreshState;


@end


// MJRefresh+FooterManger.m

#import "MJRefresh+FooterManger.h"


@implementation UITableView (MJAutoRefreshFooterManger)


static char stateKey;

- (void)setFootRefreshState:(MJFooterRefreshState)footRefreshState {

    UIWindow *window = [UIApplicationsharedApplication].keyWindow;

    [RACObserve(self.mj_footer, frame)subscribeNext:^(id x) {  //这里的意思是监视mj_footer的frame变化,可以使用kvo代替RACObserve

        CGPoint point = [selfconvertPoint:self.mj_footer.frame.origintoView:window];

        if (point.y < window.frame.size.height) {

            [(MJRefreshAutoNormalFooter *)self.mj_footersetTitle:@""forState:MJRefreshStateNoMoreData];

            [self.mj_footerendRefreshingWithNoMoreData];

        }

    }];

    

    [selfhandleFooterRefresh:footRefreshState];

    NSString *value = [NSStringstringWithFormat:@"%ld", (long)footRefreshState];

    objc_setAssociatedObject(self, &stateKey, value, OBJC_ASSOCIATION_COPY_NONATOMIC);

}


- (MJFooterRefreshState)footRefreshState {

    NSString *refreshState =objc_getAssociatedObject(self, &stateKey);

    if ([refreshStateisEqualToString:@"MJFooterRefreshStateLoadMore"]) {

        returnMJFooterRefreshStateNoMore;

    }

    else {

        returnMJFooterRefreshStateLoadMore;

    }

}


- (void) handleFooterRefresh: (MJFooterRefreshState)footRefreshState {

    MJRefreshAutoNormalFooter *footer = (MJRefreshAutoNormalFooter*)self.mj_footer;

    switch (footRefreshState) {

        caseMJFooterRefreshStateNormal: {

            [footer setTitle:@""forState:MJRefreshStateIdle];

            break;

        }

        caseMJFooterRefreshStateLoadMore: {

            [self.mj_footerendRefreshing];

            break;

        }

        caseMJFooterRefreshStateNoMore: {

            [footer setTitle:NO_MORE_DATA_TEXTforState:MJRefreshStateNoMoreData];

            [self.mj_footerendRefreshingWithNoMoreData];

            break;

        }

        default:

            break;

    }

}


@end


使用方法:

1. 在tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock之后初始化刷新状态:

eg:

_tableView.mj_footer = [MJRefreshAutoNormalFooterfooterWithRefreshingBlock:^{

  }];

 _tableView.footRefreshState =MJFooterRefreshStateNormal;


2、用_tableView.footRefreshState =MJFooterRefreshStateLoadMore;替代你代码的[_tableView.mj_footerendRefreshing];

_tableView.footRefreshState =MJFooterRefreshStateNoMore;替代你代码的[_tableView.mj_footerendRefreshingWithNoMoreData];


有问题一块讨论:qq:1246489125

你可能感兴趣的:(ios)