说明 | |
---|---|
首次发布 | 2019年04月05日 |
最近更新 | 2019年04月05日 |
背景: 为了避免写大量重复代码,故封装了该分类。本次只是封装了 MJRefresh 的常用场景,包括 Header 封装了
MJRefreshNormalHeader
的样式,Footer 封装了MJRefreshBackNormalFooter
和MJRefreshAutoNormalFooter
的样式。如果不能满足需求,可以在此基础上修改。
封装的代码
UIScrollView+MZRefresh.h
#import <UIKit/UIKit.h>
@interface UIScrollView (MZRefresh)
/** 当前页码 */
@property (nonatomic, assign) NSInteger currentPage;
/** 每页的数据,默认为20条 */
@property (nonatomic, assign) NSInteger pageSize;
/** 是否展示`最后更新时间`,默认为`YES`;如果设为`NO`,则stateLabel就居中显示 */
@property (nonatomic, assign) BOOL showLastUpdatedTimeLabel;
- (void)addHeaderWithDataBlock:(void(^)(void))block;
- (void)addAutoFooter:(BOOL)isTrue withDataBlock:(void(^)(void))block;
/** 提示没有更多数据 */
- (void)endFooterWithNoMoreData;
/** 停止header和footer刷新 */
- (void)endRefreshing;
@end
UIScrollView+MZRefresh.m
#import "UIScrollView+MZRefresh.h"
#import <objc/runtime.h>
#import <MJRefresh/MJRefresh.h>
@implementation UIScrollView (MZRefresh)
- (void)addHeaderWithDataBlock:(void(^)(void))block {
__weak typeof(self) weakself = self;
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
weakself.currentPage = 0;
weakself.pageSize = weakself.pageSize;
[weakself.mj_footer resetNoMoreData];
block();
}];
if (!self.showLastUpdatedTimeLabel) {
header.lastUpdatedTimeLabel.hidden = YES;
header.stateLabel.textAlignment = NSTextAlignmentCenter;
}
self.mj_header = header;
}
- (void)addAutoFooter:(BOOL)isTrue withDataBlock:(void(^)(void))block {
MJRefreshFooter *footer;
__weak typeof(self) weakself = self;
if (isTrue) {
footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
weakself.currentPage++;
block();
}];
} else {
footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
weakself.currentPage++;
block();
}];
}
self.mj_footer = footer;
}
- (void)endRefreshing {
[self.mj_header endRefreshing];
[self.mj_footer endRefreshing];
}
- (void)endFooterWithNoMoreData {
[self.mj_footer endRefreshingWithNoMoreData];
}
#pragma mark - Getters / Setters
- (NSInteger)currentPage {
return [objc_getAssociatedObject(self, _cmd) integerValue];
}
- (void)setCurrentPage:(NSInteger)currentPage {
objc_setAssociatedObject(self, @selector(currentPage), @(currentPage), OBJC_ASSOCIATION_RETAIN);
}
- (NSInteger)pageSize {
NSNumber *count = objc_getAssociatedObject(self, _cmd);
if (count) {
return count.integerValue;
}
self.pageSize = 20;
return 20;
}
- (void)setPageSize:(NSInteger)pageSize {
objc_setAssociatedObject(self, @selector(pageSize), @(pageSize), OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)showLastUpdatedTimeLabel {
NSNumber *show = objc_getAssociatedObject(self, _cmd);
if (show) {
return show.boolValue;
}
self.showLastUpdatedTimeLabel = YES;
return YES;
}
- (void)setShowLastUpdatedTimeLabel:(BOOL)showLastUpdatedTimeLabel {
objc_setAssociatedObject(self, @selector(showLastUpdatedTimeLabel), @(showLastUpdatedTimeLabel), OBJC_ASSOCIATION_RETAIN);
}
@end
使用示例:
#import "ViewController.h"
#import "UIScrollView+MZRefresh.h"
#import <MJRefresh/MJRefresh.h>
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, assign) NSInteger pageSize;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.mTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
self.mTableView.pageSize = self.pageSize = 10;
[self addDatas];
[self addRefresh];
}
// 添加 MJRefresh
- (void)addRefresh {
__weak typeof(self) weakself = self;
// 不展示最近刷新时间
self.mTableView.showLastUpdatedTimeLabel = NO;
// 添加 Header
[self.mTableView addHeaderWithDataBlock:^{
[weakself pullToRefresh];
}];
// 添加 Footer
[self.mTableView addAutoFooter:YES withDataBlock:^{
[weakself addDatas];
}];
}
// 模拟网络请求
- (void)addDatas {
NSUInteger count = self.dataList.count;
if (count >= 3 * self.pageSize - 1) {
// 模拟加载完全部数据
[self.mTableView endFooterWithNoMoreData];
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (NSInteger i = 0; i < self.pageSize; i++) {
[self.dataList addObject:[NSString stringWithFormat:@"%ld", count + i]];
}
// 停止刷新
[self.mTableView endRefreshing];
[self.mTableView reloadData];
});
}
- (void)pullToRefresh {
[self.dataList removeAllObjects];
[self addDatas];
}
#pragma mark - tableViewDataSource && tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = self.dataList[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
NSLog(@"ViewController 释放了");
}
- (NSMutableArray *)dataList {
if (!_dataList) {
_dataList = [NSMutableArray array];
}
return _dataList;
}
@end
网友评论