Skip to content

Commit 15155ae

Browse files
committed
用于刷新的item和model
1 parent e8fe6ce commit 15155ae

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

KtTableView/KtTableView.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
E1DCC9F81CE5A81000CDC83D /* KtBaseItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DCC9F71CE5A81000CDC83D /* KtBaseItem.m */; };
3232
E1DCC9FB1CE5A99C00CDC83D /* KtMainTableItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DCC9FA1CE5A99C00CDC83D /* KtMainTableItem.m */; };
3333
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */; };
34+
E1F64E7E1CECB048005800E4 /* KtBaseListModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E1F64E7D1CECB048005800E4 /* KtBaseListModel.m */; };
35+
E1F64E811CECB067005800E4 /* KtBaseListItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E1F64E801CECB067005800E4 /* KtBaseListItem.m */; };
3436
/* End PBXBuildFile section */
3537

3638
/* Begin PBXContainerItemProxy section */
@@ -100,6 +102,10 @@
100102
E1DCC9FA1CE5A99C00CDC83D /* KtMainTableItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtMainTableItem.m; sourceTree = "<group>"; };
101103
E1EE5C9B1CBE811C0095688B /* KtBaseTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseTableView.h; sourceTree = "<group>"; };
102104
E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseTableView.m; sourceTree = "<group>"; };
105+
E1F64E7C1CECB048005800E4 /* KtBaseListModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseListModel.h; sourceTree = "<group>"; };
106+
E1F64E7D1CECB048005800E4 /* KtBaseListModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseListModel.m; sourceTree = "<group>"; };
107+
E1F64E7F1CECB067005800E4 /* KtBaseListItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseListItem.h; sourceTree = "<group>"; };
108+
E1F64E801CECB067005800E4 /* KtBaseListItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseListItem.m; sourceTree = "<group>"; };
103109
/* End PBXFileReference section */
104110

105111
/* Begin PBXFrameworksBuildPhase section */
@@ -245,6 +251,7 @@
245251
E1DCC9EC1CE5A5FA00CDC83D /* KtBaseModel */ = {
246252
isa = PBXGroup;
247253
children = (
254+
E1F64E7B1CECB02E005800E4 /* KtBaseListModel */,
248255
E1DCC9ED1CE5A61A00CDC83D /* KtBaseModel.h */,
249256
E1DCC9EE1CE5A61A00CDC83D /* KtBaseModel.m */,
250257
E1DCC9F61CE5A81000CDC83D /* KtBaseItem.h */,
@@ -255,6 +262,17 @@
255262
path = KtBaseModel;
256263
sourceTree = "<group>";
257264
};
265+
E1F64E7B1CECB02E005800E4 /* KtBaseListModel */ = {
266+
isa = PBXGroup;
267+
children = (
268+
E1F64E7F1CECB067005800E4 /* KtBaseListItem.h */,
269+
E1F64E801CECB067005800E4 /* KtBaseListItem.m */,
270+
E1F64E7C1CECB048005800E4 /* KtBaseListModel.h */,
271+
E1F64E7D1CECB048005800E4 /* KtBaseListModel.m */,
272+
);
273+
path = KtBaseListModel;
274+
sourceTree = "<group>";
275+
};
258276
F71B31402A7F180F563A44F9 /* Frameworks */ = {
259277
isa = PBXGroup;
260278
children = (
@@ -452,10 +470,12 @@
452470
E108B2AB1CBE14C300191E0B /* KtTableViewDataSource.m in Sources */,
453471
E108B2B71CBE1F6500191E0B /* KtTableViewBaseItem.m in Sources */,
454472
E108B2B11CBE191300191E0B /* KtBaseTableViewCell.m in Sources */,
473+
E1F64E811CECB067005800E4 /* KtBaseListItem.m in Sources */,
455474
E1DCC9EF1CE5A61A00CDC83D /* KtBaseModel.m in Sources */,
456475
E1DCC9F21CE5A6A000CDC83D /* KtBaseServerAPI.m in Sources */,
457476
E1621E081CBDF267003D9438 /* KTMainViewController.m in Sources */,
458477
E1DCC9FB1CE5A99C00CDC83D /* KtMainTableItem.m in Sources */,
478+
E1F64E7E1CECB048005800E4 /* KtBaseListModel.m in Sources */,
459479
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */,
460480
E1621E051CBDF267003D9438 /* AppDelegate.m in Sources */,
461481
E108B2AE1CBE15D800191E0B /* KtTableViewSectionObject.m in Sources */,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// KtBaseListItem.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/18.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseItem.h"
10+
11+
@interface KtBaseListItem : KtBaseItem
12+
13+
//@property (nonatomic, assign) int hasNext;
14+
//@property (nonatomic, assign) int hasPrev;
15+
@property (nonatomic, assign) int pageNumber; // 为了简化问题,我们让客户端直接传这个值,其实应该用上面两个。
16+
17+
@end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// KtBaseListItem.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/18.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseListItem.h"
10+
11+
@implementation KtBaseListItem
12+
13+
- (id)initWithData:(NSDictionary *)data {
14+
if (self = [super initWithData:data]) {
15+
self.pageNumber = [[NSString stringWithFormat:@"%@", [data objectForKey:@"page_number"]] intValue];
16+
}
17+
return self;
18+
}
19+
20+
@end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// KtBaseListModel.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/18.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseModel.h"
10+
#import "KtBaseListItem.h"
11+
12+
@protocol KtBaseListModelProtocol <NSObject>
13+
14+
@required
15+
- (void)refreshRequestDidSuccess;
16+
- (void)loadRequestDidSuccess;
17+
- (void)didLoadLastPage;
18+
- (void)handleAfterRequestFinish; // 请求结束后的操作,刷新tableview或关闭动画等。
19+
20+
@optional
21+
- (void)didLoadFirstPage;
22+
23+
@end
24+
25+
@interface KtBaseListModel : KtBaseModel
26+
27+
@property (nonatomic, strong) KtBaseListItem *listItem;
28+
@property (nonatomic, weak) id<KtBaseListModelProtocol> delegate;
29+
@property (nonatomic, assign) BOOL isRefresh; // 如果为是,表示刷新,否则为加载。
30+
31+
- (void)loadPage:(int)pageNumber;
32+
- (void)loadNextPage;
33+
- (void)loadPreviousPage;
34+
35+
@end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// KtBaseListModel.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/18.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseListModel.h"
10+
11+
@interface KtBaseListModel ()
12+
13+
@property (nonatomic, assign) int currentPage;
14+
@property (nonatomic, strong) NSCache *cache; // 目前先用系统的缓存来代替
15+
16+
@end
17+
18+
@implementation KtBaseListModel
19+
20+
- (id)initWithAddress:(NSString *)address {
21+
self = [super initWithAddress:address];
22+
if (self) {
23+
self.currentPage = 0;
24+
self.cache = [[NSCache alloc] init];
25+
}
26+
return self;
27+
}
28+
29+
- (void)setDelegate:(id<KtBaseListModelProtocol>)delegate {
30+
_delegate = delegate;
31+
__weak typeof(self)wself = self;
32+
[self setCompletionBlock:^(KtBaseModel *model) {
33+
typeof(self)sself = wself;
34+
if (sself.isRefresh && [sself.delegate respondsToSelector:@selector(refreshRequestDidSuccess)]) {
35+
[sself.delegate refreshRequestDidSuccess];
36+
}
37+
else if (!sself.isRefresh && [sself.delegate respondsToSelector:@selector(refreshRequestDidSuccess)]) {
38+
[sself.delegate loadRequestDidSuccess];
39+
}
40+
[sself.delegate handleAfterRequestFinish];
41+
sself.isRefresh = NO;
42+
}];
43+
}
44+
45+
- (void)handleParsedData:(KtBaseItem *)parsedData {
46+
if ([parsedData isKindOfClass:[KtBaseListItem class]]) {
47+
self.listItem = (KtBaseListItem *)parsedData;
48+
[self.cache setObject:self.listItem forKey:[NSString stringWithFormat:@"%d", self.currentPage]];
49+
}
50+
}
51+
52+
- (void)loadPage:(int)pageNumber {
53+
NSMutableDictionary *newDic = [NSMutableDictionary dictionaryWithDictionary:self.params];
54+
[newDic setObject:@(pageNumber) forKey:@"nextPage"];
55+
self.params = newDic;
56+
57+
self.currentPage = pageNumber;
58+
KtBaseListItem *cachedItem = [self.cache objectForKey:[NSString stringWithFormat:@"%d", self.currentPage]];
59+
if (cachedItem) {
60+
[self handleParsedData:cachedItem];
61+
self.completionBlock(self);
62+
}
63+
else {
64+
[self loadWithShortConnection];
65+
}
66+
}
67+
68+
- (void)loadNextPage {
69+
self.isRefresh = NO;
70+
if (self.currentPage < self.listItem.pageNumber) {
71+
++self.currentPage;
72+
[self loadPage:self.currentPage];
73+
}
74+
else {
75+
// 没有更多了。
76+
[self.delegate didLoadLastPage];
77+
}
78+
}
79+
80+
- (void)loadPreviousPage {
81+
self.isRefresh = NO;
82+
if (self.currentPage > 0) {
83+
--self.currentPage;
84+
[self loadPage:self.currentPage];
85+
}
86+
else {
87+
[self refresh]; // 如果现在是第一页,就只刷新
88+
}
89+
}
90+
91+
- (void)refresh {
92+
self.isRefresh = YES;
93+
[self loadPage:0];
94+
}
95+
96+
@end

0 commit comments

Comments
 (0)