接口部分
@protocol UITableViewPageDelegate <NSObject>
@optional
/*
下拉调用
*/
-(void)pullDownRefreshInTableView:(UITableView *_Nonnull)tableView;
/*
上拉调用
*/
-(void)tableView:(UITableView *_Nonnull)tableView pullUpWithPage:(long)page;
@end
@interface PageTableView : UITableView
@property (nonatomic, weak) id<UITableViewPageDelegate> pageDelegate;
#pragma mark - 数据处理
/*
allData,分页请求的完整数据
*/
@property(nonatomic, strong, readonly) NSArray<id> *allData;
/*
添加页面数据
*/
-(void)addCurrentPageData:(NSArray<id> *)currentPageData;
/*
用于判断是否最后一页
*/
-(void)upgradeCurrentPageCount:(long)currentPageCount;
#pragma mark - 下拉刷新
/*
赋值给mj_header即可使用
*/
@property(nonatomic, strong, readonly) MJRefreshNormalHeader *normalHeader;
/*
下拉刷新结束需要调用
*/
-(void)endPullDownRefresh;
#pragma mark - 上拉刷新
/*
赋值给mj_footer即可使用
*/
@property(nonatomic, strong, readonly) MJRefreshAutoNormalFooter *normalFooter;
/*
是否允许上拉
默认不允许,为了良好的用户体验,第一页数据加载出来之前不能进行上拉,当从第二页开始的时候,会自动置为允许
*/
@property(nonatomic, assign) bool allowPullUp;
/*
currentPage代表当前即将要请求的页,即还未请求
*/
@property(nonatomic, assign, readonly) long currentPage;
@property(nonatomic, assign, readonly) bool lastPage;
/*
设置pageSize可帮助判断lastPage,与totalCount配合使用
*/
@property(nonatomic, assign) long pageSize;
/*
支持设置两种总数判断,任选其一
*/
@property(nonatomic, assign) long totalPage;
@property(nonatomic, assign) long totalCount;
/*
上拉加载结束需要调用
*/
-(void)endPullUpRefresh;
/*
该操作会迭代下一页
*/
-(void)goNextPage;
/*
重置上拉刷新,同时会重置currentPage为1
*/
-(void)resetPullUpRefresh;
@end
实现部分
@interface PageTableView ()
@property(nonatomic, strong) NSMutableArray<id> *mutableArray;
@property(nonatomic, assign) long currentPageCount;
@end
@implementation PageTableView
{
long _currentPage;
MJRefreshNormalHeader *_normalHeader;
MJRefreshAutoNormalFooter *_normalFooter;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
_currentPage = 1;
self.currentPageCount = -1;
self.pageSize = 0;
self.totalPage = -1;
self.totalCount = -1;
self.allowPullUp = NO;
}
return self;
}
#pragma mark - 懒加载/setter/getter
- (MJRefreshNormalHeader *)normalHeader
{
if (_normalHeader == nil) {
_normalHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewPullDownAction)];
[_normalHeader setTitle:@"下拉进行更新" forState:MJRefreshStateIdle];
[_normalHeader setTitle:@"松开立即更新" forState:MJRefreshStatePulling];
[_normalHeader setTitle:@"更新中" forState:MJRefreshStateRefreshing];
_normalHeader.lastUpdatedTimeLabel.hidden = YES;
}
return _normalHeader;
}
-(MJRefreshAutoNormalFooter *)normalFooter
{
if (_normalFooter == nil) {
_normalFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(tableViewPullUpAction)];
[_normalFooter setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
[_normalFooter setTitle:@"加载中" forState:MJRefreshStateRefreshing];
[_normalFooter setTitle:@"———— 没有更多信息了 ————" forState:MJRefreshStateNoMoreData];
_normalFooter.stateLabel.textColor = [UIColor corlorWithR:200 G:200 B:200];
//默认初始化不显示
_normalFooter.stateLabel.hidden = YES;
}
return _normalFooter;
}
-(NSArray<id> *)allData
{
return [self.mutableArray copy];
}
-(NSMutableArray<id> *)mutableArray
{
if (_mutableArray == nil) {
_mutableArray = [NSMutableArray array];
}
return _mutableArray;
}
-(long)currentPage
{
return _currentPage;
}
-(bool)lastPage
{
if (self.pageSize > 0 && (self.currentPageCount >= 0 && self.currentPageCount < self.pageSize)) {
return YES;
}
//优先使用已加载数据判断是否最后一页(若已加载数据存在)
if ((self.totalCount >= 0 && self.mutableArray.count > 0) && self.mutableArray.count >= self.totalCount) {
return YES;
}
long lastLoadPage = self.currentPage - 1;
if ((self.totalPage >= 0) && lastLoadPage >= self.totalPage) {
return YES;
}
if ((self.totalCount >= 0 && self.pageSize > 0) && lastLoadPage * self.pageSize >= self.totalCount) {
return YES;
}
return NO;
}
-(void)setAllowPullUp:(bool)allowPullUp
{
_allowPullUp = allowPullUp;
if (self.mj_footer && [self.mj_footer isKindOfClass:[MJRefreshAutoNormalFooter class]]) {
MJRefreshAutoNormalFooter *normalFooter = (MJRefreshAutoNormalFooter *)self.mj_footer;
normalFooter.stateLabel.hidden = !allowPullUp;
}
}
#pragma mark - 外部方法
-(void)addCurrentPageData:(NSArray<id> *)currentPageData
{
if (self.currentPage == 1) {
[self.mutableArray removeAllObjects];
}
if (currentPageData.count > 0) {
[self.mutableArray addObjectsFromArray:currentPageData];
}
[self upgradeCurrentPageCount:currentPageData.count];
}
-(void)upgradeCurrentPageCount:(long)currentPageCount
{
if (currentPageCount >= 0) {
self.currentPageCount = currentPageCount;
}
}
-(void)endPullDownRefresh
{
if (self.mj_header.state == MJRefreshStateRefreshing) {
[self.mj_header endRefreshing];
}
}
-(void)endPullUpRefresh
{
if (self.mj_footer) {
if (self.lastPage) {
//最后一页,结束刷新并展示无更多数据
[self.mj_footer endRefreshingWithNoMoreData];
}
else
{
//结束刷新
[self.mj_footer endRefreshing];
}
}
}
-(void)goNextPage
{
_currentPage = _currentPage + 1;
if (self.currentPage > 1) {
self.allowPullUp = YES;
}
}
-(void)resetPullUpRefresh
{
self.allowPullUp = NO;
_currentPage = 1;
self.currentPageCount = -1;
if (self.mj_footer) {
self.mj_footer.state = MJRefreshStateIdle;
}
}
#pragma mark - 内部方法
-(void)tableViewPullDownAction
{
//下拉刷新需要重置上拉操作
[self resetPullUpRefresh];
if (self.pageDelegate && [self.delegate respondsToSelector:@selector(pullDownRefreshInTableView:)]) {
[self.pageDelegate pullDownRefreshInTableView:self];
}
}
-(void)tableViewPullUpAction
{
if (self.allowPullUp == NO) {
//不允许上拉加载更多
self.mj_footer.state = MJRefreshStateIdle;
return;
}
if (self.pageDelegate && [self.delegate respondsToSelector:@selector(tableView:pullUpWithPage:)]) {
[self.pageDelegate tableView:self pullUpWithPage:self.currentPage];
}
}
@end
网友评论