Skip to content

Commit e8f2e45

Browse files
committed
设置好数据源的基类
1 parent 1d9c1fe commit e8f2e45

5 files changed

Lines changed: 210 additions & 0 deletions

File tree

KtTableView/KtTableView.xcodeproj/project.pbxproj

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
/* Begin PBXBuildFile section */
1010
E108B2A71CBE0A2600191E0B /* KtMainTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2A61CBE0A2600191E0B /* KtMainTableViewCell.m */; };
11+
E108B2AB1CBE14C300191E0B /* KtTableViewDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2AA1CBE14C300191E0B /* KtTableViewDataSource.m */; };
12+
E108B2AE1CBE15D800191E0B /* KtTableViewSectionObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2AD1CBE15D800191E0B /* KtTableViewSectionObject.m */; };
13+
E108B2B11CBE191300191E0B /* KtBaseTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2B01CBE191300191E0B /* KtBaseTableViewCell.m */; };
14+
E108B2B41CBE1C9100191E0B /* KtMainTableViewDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2B31CBE1C9100191E0B /* KtMainTableViewDataSource.m */; };
15+
E108B2B71CBE1F6500191E0B /* KtTableViewBaseItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E108B2B61CBE1F6500191E0B /* KtTableViewBaseItem.m */; };
1116
E1621E021CBDF265003D9438 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E011CBDF265003D9438 /* main.m */; };
1217
E1621E051CBDF267003D9438 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E041CBDF267003D9438 /* AppDelegate.m */; };
1318
E1621E081CBDF267003D9438 /* KTMainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E071CBDF267003D9438 /* KTMainViewController.m */; };
@@ -16,6 +21,7 @@
1621
E1621E101CBDF267003D9438 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E1621E0E1CBDF267003D9438 /* LaunchScreen.storyboard */; };
1722
E1621E1B1CBDF267003D9438 /* KtTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E1A1CBDF267003D9438 /* KtTableViewTests.m */; };
1823
E1621E261CBDF267003D9438 /* KtTableViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E251CBDF267003D9438 /* KtTableViewUITests.m */; };
24+
E1EE5C9A1CBE791E0095688B /* UIView+KtExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = E1EE5C991CBE791E0095688B /* UIView+KtExtension.m */; };
1925
/* End PBXBuildFile section */
2026

2127
/* Begin PBXContainerItemProxy section */
@@ -38,6 +44,16 @@
3844
/* Begin PBXFileReference section */
3945
E108B2A51CBE0A2600191E0B /* KtMainTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtMainTableViewCell.h; sourceTree = "<group>"; };
4046
E108B2A61CBE0A2600191E0B /* KtMainTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtMainTableViewCell.m; sourceTree = "<group>"; };
47+
E108B2A91CBE14C300191E0B /* KtTableViewDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtTableViewDataSource.h; sourceTree = "<group>"; };
48+
E108B2AA1CBE14C300191E0B /* KtTableViewDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtTableViewDataSource.m; sourceTree = "<group>"; };
49+
E108B2AC1CBE15D800191E0B /* KtTableViewSectionObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtTableViewSectionObject.h; sourceTree = "<group>"; };
50+
E108B2AD1CBE15D800191E0B /* KtTableViewSectionObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtTableViewSectionObject.m; sourceTree = "<group>"; };
51+
E108B2AF1CBE191300191E0B /* KtBaseTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseTableViewCell.h; sourceTree = "<group>"; };
52+
E108B2B01CBE191300191E0B /* KtBaseTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseTableViewCell.m; sourceTree = "<group>"; };
53+
E108B2B21CBE1C9100191E0B /* KtMainTableViewDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtMainTableViewDataSource.h; sourceTree = "<group>"; };
54+
E108B2B31CBE1C9100191E0B /* KtMainTableViewDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtMainTableViewDataSource.m; sourceTree = "<group>"; };
55+
E108B2B51CBE1F6500191E0B /* KtTableViewBaseItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtTableViewBaseItem.h; sourceTree = "<group>"; };
56+
E108B2B61CBE1F6500191E0B /* KtTableViewBaseItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtTableViewBaseItem.m; sourceTree = "<group>"; };
4157
E1621DFD1CBDF265003D9438 /* KtTableView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KtTableView.app; sourceTree = BUILT_PRODUCTS_DIR; };
4258
E1621E011CBDF265003D9438 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
4359
E1621E031CBDF267003D9438 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -54,6 +70,8 @@
5470
E1621E211CBDF267003D9438 /* KtTableViewUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KtTableViewUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5571
E1621E251CBDF267003D9438 /* KtTableViewUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KtTableViewUITests.m; sourceTree = "<group>"; };
5672
E1621E271CBDF267003D9438 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
73+
E1EE5C981CBE791E0095688B /* UIView+KtExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+KtExtension.h"; sourceTree = "<group>"; };
74+
E1EE5C991CBE791E0095688B /* UIView+KtExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+KtExtension.m"; sourceTree = "<group>"; };
5775
/* End PBXFileReference section */
5876

5977
/* Begin PBXFrameworksBuildPhase section */
@@ -81,6 +99,21 @@
8199
/* End PBXFrameworksBuildPhase section */
82100

83101
/* Begin PBXGroup section */
102+
E108B2A81CBE148200191E0B /* KtBaseTableViewController */ = {
103+
isa = PBXGroup;
104+
children = (
105+
E108B2A91CBE14C300191E0B /* KtTableViewDataSource.h */,
106+
E108B2AA1CBE14C300191E0B /* KtTableViewDataSource.m */,
107+
E108B2AF1CBE191300191E0B /* KtBaseTableViewCell.h */,
108+
E108B2B01CBE191300191E0B /* KtBaseTableViewCell.m */,
109+
E108B2AC1CBE15D800191E0B /* KtTableViewSectionObject.h */,
110+
E108B2AD1CBE15D800191E0B /* KtTableViewSectionObject.m */,
111+
E108B2B51CBE1F6500191E0B /* KtTableViewBaseItem.h */,
112+
E108B2B61CBE1F6500191E0B /* KtTableViewBaseItem.m */,
113+
);
114+
name = KtBaseTableViewController;
115+
sourceTree = "<group>";
116+
};
84117
E1621DF41CBDF265003D9438 = {
85118
isa = PBXGroup;
86119
children = (
@@ -104,10 +137,14 @@
104137
E1621DFF1CBDF265003D9438 /* KtTableView */ = {
105138
isa = PBXGroup;
106139
children = (
140+
E1EE5C961CBE78BD0095688B /* KtExtension */,
141+
E108B2A81CBE148200191E0B /* KtBaseTableViewController */,
107142
E1621E031CBDF267003D9438 /* AppDelegate.h */,
108143
E1621E041CBDF267003D9438 /* AppDelegate.m */,
109144
E1621E061CBDF267003D9438 /* KTMainViewController.h */,
110145
E1621E071CBDF267003D9438 /* KTMainViewController.m */,
146+
E108B2B21CBE1C9100191E0B /* KtMainTableViewDataSource.h */,
147+
E108B2B31CBE1C9100191E0B /* KtMainTableViewDataSource.m */,
111148
E108B2A51CBE0A2600191E0B /* KtMainTableViewCell.h */,
112149
E108B2A61CBE0A2600191E0B /* KtMainTableViewCell.m */,
113150
E1621E091CBDF267003D9438 /* Main.storyboard */,
@@ -145,6 +182,15 @@
145182
path = KtTableViewUITests;
146183
sourceTree = "<group>";
147184
};
185+
E1EE5C961CBE78BD0095688B /* KtExtension */ = {
186+
isa = PBXGroup;
187+
children = (
188+
E1EE5C981CBE791E0095688B /* UIView+KtExtension.h */,
189+
E1EE5C991CBE791E0095688B /* UIView+KtExtension.m */,
190+
);
191+
name = KtExtension;
192+
sourceTree = "<group>";
193+
};
148194
/* End PBXGroup section */
149195

150196
/* Begin PBXNativeTarget section */
@@ -275,8 +321,14 @@
275321
isa = PBXSourcesBuildPhase;
276322
buildActionMask = 2147483647;
277323
files = (
324+
E108B2B41CBE1C9100191E0B /* KtMainTableViewDataSource.m in Sources */,
325+
E108B2AB1CBE14C300191E0B /* KtTableViewDataSource.m in Sources */,
326+
E108B2B71CBE1F6500191E0B /* KtTableViewBaseItem.m in Sources */,
327+
E1EE5C9A1CBE791E0095688B /* UIView+KtExtension.m in Sources */,
328+
E108B2B11CBE191300191E0B /* KtBaseTableViewCell.m in Sources */,
278329
E1621E081CBDF267003D9438 /* KTMainViewController.m in Sources */,
279330
E1621E051CBDF267003D9438 /* AppDelegate.m in Sources */,
331+
E108B2AE1CBE15D800191E0B /* KtTableViewSectionObject.m in Sources */,
280332
E1621E021CBDF265003D9438 /* main.m in Sources */,
281333
E108B2A71CBE0A2600191E0B /* KtMainTableViewCell.m in Sources */,
282334
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// KtTableViewDataSource.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <UIKit/UIKit.h>
11+
12+
@class KtTableViewBaseItem;
13+
14+
@protocol KtTableViewDataSource <UITableViewDataSource>
15+
16+
@optional
17+
18+
- (KtTableViewBaseItem *)tableView:(UITableView *)tableView objectForRowAtIndexPath:(NSIndexPath *)indexPath;
19+
- (Class)tableView:(UITableView*)tableView cellClassForObject:(KtTableViewBaseItem *)object;
20+
21+
@end
22+
23+
@interface KtTableViewDataSource : NSObject<KtTableViewDataSource>
24+
25+
@property (nonatomic, strong) NSMutableArray *sections; // 二维数组,每个元素都是一个 SectionObject
26+
27+
@end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// KtTableViewDataSource.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtTableViewDataSource.h"
10+
#import "KtTableViewSectionObject.h"
11+
#import "KtBaseTableViewCell.h"
12+
#import "KtTableViewBaseItem.h"
13+
14+
#import <objc/runtime.h>
15+
16+
@implementation KtTableViewDataSource
17+
18+
#pragma mark - KtTableViewDataSource
19+
- (KtTableViewBaseItem *)tableView:(UITableView *)tableView objectForRowAtIndexPath:(NSIndexPath *)indexPath {
20+
if (self.sections.count > indexPath.section) {
21+
KtTableViewSectionObject *sectionObject = [self.sections objectAtIndex:indexPath.section];
22+
if ([sectionObject.items count] > indexPath.row) {
23+
return [sectionObject.items objectAtIndex:indexPath.row];
24+
}
25+
}
26+
return nil;
27+
}
28+
29+
- (Class)tableView:(UITableView*)tableView cellClassForObject:(KtTableViewBaseItem *)object { // 这个方法会子类有机会重写,默认的 Cell 类型是 KtBaseTableViewCell
30+
return [KtBaseTableViewCell class];
31+
}
32+
33+
#pragma mark - UITableViewDataSource Required
34+
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
35+
if (self.sections.count > section) {
36+
KtTableViewSectionObject *sectionObject = [self.sections objectAtIndex:section];
37+
return sectionObject.items.count;
38+
}
39+
return 0;
40+
}
41+
42+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
43+
KtTableViewBaseItem *object = [self tableView:tableView objectForRowAtIndexPath:indexPath];
44+
Class cellClass = [self tableView:tableView cellClassForObject:object];
45+
NSString *className = [NSString stringWithUTF8String:class_getName(cellClass)];
46+
47+
KtBaseTableViewCell* cell = (KtBaseTableViewCell*)[tableView dequeueReusableCellWithIdentifier:className];
48+
if (!cell) {
49+
cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:className];
50+
}
51+
[cell setObject:object];
52+
53+
return cell;
54+
}
55+
56+
#pragma mark - UITableViewDataSource Optional
57+
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
58+
return self.sections ? self.sections.count : 0;
59+
}
60+
61+
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
62+
if (self.sections.count > section) {
63+
KtTableViewSectionObject *sectionObject = [self.sections objectAtIndex:section];
64+
return sectionObject.headerTitle;
65+
}
66+
return nil;
67+
}
68+
69+
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
70+
if (self.sections.count > section) {
71+
KtTableViewSectionObject *sectionObject = [self.sections objectAtIndex:section];
72+
if (sectionObject != nil && sectionObject.footerTitle != nil && ![sectionObject.footerTitle isEqualToString:@""]) {
73+
return sectionObject.footerTitle;
74+
}
75+
}
76+
return nil;
77+
}
78+
79+
80+
@end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// KtTableViewSectionObject.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface KtTableViewSectionObject : NSObject
12+
13+
@property (nonatomic, copy) NSString *headerTitle; // UITableDataSource 协议中的 titleForHeaderInSection 方法可能会用到
14+
@property (nonatomic, copy) NSString *footerTitle; // UITableDataSource 协议中的 titleForFooterInSection 方法可能会用到
15+
16+
@property (nonatomic, retain) NSMutableArray *items;
17+
18+
- (instancetype)initWithItemArray:(NSMutableArray *)items;
19+
20+
@end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// KtTableViewSectionObject.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtTableViewSectionObject.h"
10+
11+
@implementation KtTableViewSectionObject
12+
13+
- (instancetype)init {
14+
self = [super init];
15+
if (self) {
16+
self.headerTitle = @"";
17+
self.footerTitle = @"";
18+
self.items = [[NSMutableArray alloc] init];
19+
}
20+
return self;
21+
}
22+
23+
- (instancetype)initWithItemArray:(NSMutableArray *)items {
24+
self = [self init];
25+
if (self) {
26+
[self.items addObjectsFromArray:items];
27+
}
28+
return self;
29+
}
30+
31+
@end

0 commit comments

Comments
 (0)