MJRefresh 封装
2018-08-08 本文已影响347人
赵哥窟
MJRefresh是很好用的一个刷新控件,但是如果在每个ViewController中都加入如下代码
self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//Call this Block When enter the refresh status automatically
}];
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//Call this Block When enter the refresh status automatically
}];
代码复用性不高,而且每个ViewController 在加载更多的时候还要管理Page
所以想一个办法对MJRefresh进行二次封装,在使用的时候方便调用。
MJRefresh 支持TableView,CollectionView,Scrollview,很容易联想到TableView,CollectionView都是继续Scrollview,那我们写一个Scrollview的Category是不是就可以
接下来我们需要考虑两个问题,Category是不可以直接添加属性的,那我们怎么添加Page和刷新Block这两个属性。相信都知道用RunTime 关联属性不就可以了。
所以我们在Category中声明属性
typedef void(^RefreshBlock)(NSInteger pageIndex);
typedef void(^LoadMoreBlock)(NSInteger pageIndex);
@interface UIScrollView()
/**页码*/
@property (assign, nonatomic) NSInteger pageIndex;
/**下拉时候触发的block*/
@property (nonatomic, copy) RefreshBlock refreshBlock;
/**上拉时候触发的block*/
@property (nonatomic, copy) LoadMoreBlock loadMoreBlock;
@end
然后用RunTime添加属性
static void *pagaIndexKey = &pagaIndexKey;
- (void)setPageIndex:(NSInteger)pageIndex{
objc_setAssociatedObject(self, &pagaIndexKey, @(pageIndex), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)pageIndex
{
return [objc_getAssociatedObject(self, &pagaIndexKey) integerValue];
}
static void *RefreshBlockKey = &RefreshBlockKey;
- (void)setRefreshBlock:(void (^)(void))RefreshBlock{
objc_setAssociatedObject(self, &RefreshBlockKey, RefreshBlock, OBJC_ASSOCIATION_COPY);
}
- (RefreshBlock)refreshBlock
{
return objc_getAssociatedObject(self, &RefreshBlockKey);
}
static void *LoadMoreBlockKey = &LoadMoreBlockKey;
- (void)setLoadMoreBlock:(LoadMoreBlock)loadMoreBlock{
objc_setAssociatedObject(self, &LoadMoreBlockKey, loadMoreBlock, OBJC_ASSOCIATION_COPY);
}
- (LoadMoreBlock)loadMoreBlock
{
return objc_getAssociatedObject(self, &LoadMoreBlockKey);
}
接下来就是添加两个刷新和加载更多的方法
- (void)addHeaderWithHeaderWithBeginRefresh:(BOOL)beginRefresh animation:(BOOL)animation refreshBlock:(void(^)(NSInteger pageIndex))refreshBlock{
__weak typeof(self) weakSelf = self;
self.refreshBlock = refreshBlock;
MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf resetPageNum];
if (weakSelf.refreshBlock) {
weakSelf.refreshBlock(weakSelf.pageIndex);
}
[weakSelf endHeaderRefresh];
}];
if (beginRefresh && animation) {
//有动画的刷新
[self beginHeaderRefresh];
}else if (beginRefresh && !animation){
//刷新,但是没有动画
[self.mj_header executeRefreshingCallback];
}
header.mj_h = 70.0;
self.mj_header = header;
}
- (void)addFooterWithWithHeaderWithAutomaticallyRefresh:(BOOL)automaticallyRefresh loadMoreBlock:(void(^)(NSInteger pageIndex))loadMoreBlock{
self.loadMoreBlock = loadMoreBlock;
if (automaticallyRefresh) {
MJRefreshAutoNormalFooter * footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
self.pageIndex += 1;
if (self.loadMoreBlock) {
self.loadMoreBlock(self.pageIndex);
}
[self endFooterRefresh];
}];
footer.automaticallyRefresh = automaticallyRefresh;
footer.stateLabel.font = [UIFont systemFontOfSize:13.0];
footer.stateLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
[footer setTitle:@"加载中…" forState:MJRefreshStateRefreshing];
[footer setTitle:@"这是我的底线啦~" forState:MJRefreshStateNoMoreData];
self.mj_footer = footer;
}
else{
MJRefreshBackNormalFooter * footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
self.pageIndex += 1;
if (self.loadMoreBlock) {
self.loadMoreBlock(self.pageIndex);
}
[self endFooterRefresh];
}];
footer.stateLabel.font = [UIFont systemFontOfSize:13.0];
footer.stateLabel.textColor = [UIColor colorWithWhite:0.400 alpha:1.000];
[footer setTitle:@"加载中…" forState:MJRefreshStateRefreshing];
[footer setTitle:@"这是我的底线啦~" forState:MJRefreshStateNoMoreData];
self.mj_footer = footer;
}
}
-(void)beginHeaderRefresh {
[self resetPageNum];
[self.mj_header beginRefreshing];
}
- (void)resetPageNum {
self.pageIndex = 0;
}
- (void)resetNoMoreData {
[self.mj_footer resetNoMoreData];
}
-(void)endHeaderRefresh {
[self.mj_header endRefreshing];
[self resetNoMoreData];
}
-(void)endFooterRefresh {
[self.mj_footer endRefreshing];
}
调用,是不是很简单,都不需要单独管理Page
[self.tableView addHeaderWithHeaderWithBeginRefresh:YES animation:YES refreshBlock:^(NSInteger pageIndex) {
NSLog(@"pageIndex:%zd",pageIndex);
}];
[self.tableView addFooterWithWithHeaderWithAutomaticallyRefresh:NO loadMoreBlock:^(NSInteger pageIndex) {
NSLog(@"pageIndex:%zd",pageIndex);
}];