-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+Pagination.m
executable file
·167 lines (138 loc) · 4.59 KB
/
UIViewController+Pagination.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// UIViewController+Pagination.m
// Pagination
//
// Created by eden on 16/10/9.
// Copyright © 2016年 eden. All rights reserved.
//
#import "UIViewController+Pagination.h"
#import "MJRefresh.h"
#import "objc/runtime.h"
static char currentPageKey;
static char pageSizeKey;
static char loadingPagekey;
static char scollViewKey;
#define startingPage 1 //The starting page number for your server
#define unusedLoadingPage -1 //the value of unloaded data for loadingPage
#define defaultPageSize 20 //the default value for pageSize
@interface UIViewController()
///The page number in loading, a tmp variable when loading, is only assigned to the currentPage when the load is successful, and the value of unloaded data is -1
@property (nonatomic, assign) NSInteger loadingPage;
///The scollView who been add pull to refresh operation
@property (nonatomic, assign) UIScrollView *scollView;
@end
@implementation UIViewController (Pagination)
#pragma mark - Access
- (void)setCurrentPage:(NSInteger)currentPage
{
objc_setAssociatedObject(self, ¤tPageKey, @(currentPage), OBJC_ASSOCIATION_RETAIN);
}
- (NSInteger)currentPage
{
NSNumber *currentPage = objc_getAssociatedObject(self, ¤tPageKey);
if (currentPage == nil) {
self.currentPage = startingPage;
return startingPage;
}
return [currentPage integerValue];
}
- (void)setPageSize:(NSInteger)pageSize
{
objc_setAssociatedObject(self, &pageSizeKey, @(pageSize), OBJC_ASSOCIATION_RETAIN);
}
- (NSInteger)pageSize
{
NSNumber *pageSize = objc_getAssociatedObject(self, &pageSizeKey);
if (pageSize == nil) {
self.pageSize = defaultPageSize;
return defaultPageSize;
}
return [pageSize integerValue];
}
- (void)setLoadingPage:(NSInteger)loadingPage
{
objc_setAssociatedObject(self, &loadingPagekey, @(loadingPage), OBJC_ASSOCIATION_RETAIN);
}
- (NSInteger)loadingPage
{
NSNumber *loadingPage = objc_getAssociatedObject(self, &loadingPagekey);
if (loadingPage == nil) {
self.loadingPage = unusedLoadingPage;
return unusedLoadingPage;
}
return [loadingPage integerValue];
}
- (void)setScollView:(UIScrollView *)scollView
{
objc_setAssociatedObject(self, &scollViewKey, scollView, OBJC_ASSOCIATION_RETAIN);
}
- (UIScrollView *)scollView
{
return objc_getAssociatedObject(self, &scollViewKey);
}
#pragma mark - functions
- (void)addPullDownToRefreshWithScollView:(UIScrollView*)scollView
refreshingHandler:(EDHRefreshingHandler)refreshingHandler
{
__weak typeof(self) wSelf = self;
scollView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^() {
wSelf.loadingPage = startingPage;
refreshingHandler(wSelf.loadingPage);
}];
self.scollView = scollView;
}
- (void)removePullDownToRefreshWithScollView:(UIScrollView*)scollView {
scollView.mj_header = nil;
}
- (void)addPullUpToLoadMoreWithScollView:(UIScrollView*)scollView
refreshingHandler:(EDHRefreshingHandler)refreshingHandler
{
__weak typeof(self) wSelf = self;
MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^() {
wSelf.loadingPage = wSelf.currentPage + 1;
refreshingHandler(wSelf.loadingPage);
}];
//for iphone X, make footer out of screen
CGSize size = [[UIScreen mainScreen] bounds].size;
size = CGSizeMake(size.width * [UIScreen mainScreen].scale,
size.height * [UIScreen mainScreen].scale);
if (size.height == 2436) {
footer.ignoredScrollViewContentInsetBottom = 34.0f;
}
scollView.mj_footer = footer;
self.scollView = scollView;
}
- (void)removePullUpToLoadMoreWithScollView:(UIScrollView*)scollView {
scollView.mj_footer = nil;
}
- (void)endRefreshingWithWithSucceed:(BOOL)succeed noMoreData:(BOOL)noMoreData
{
[self endRefreshing];
if (succeed) {
if (self.loadingPage == unusedLoadingPage) {
self.currentPage = startingPage;
} else {
self.currentPage = self.loadingPage;
}
}
self.loadingPage = unusedLoadingPage;
if (noMoreData) {
[self.scollView.mj_footer endRefreshingWithNoMoreData];
} else {
[self.scollView.mj_footer resetNoMoreData];
}
}
- (void)endRefreshing
{
if (self.scollView.mj_header.state == MJRefreshStateRefreshing) {
[self.scollView.mj_header endRefreshing];
}
if (self.scollView.mj_footer.state == MJRefreshStateRefreshing) {
[self.scollView.mj_footer endRefreshing];
}
}
- (void)startRefreshing
{
[self.scollView.mj_header beginRefreshing];
}
@end