Skip to content

Commit d2a8238

Browse files
committed
基类TableView 和TableViewDelegate改造
实际使用
1 parent 6475496 commit d2a8238

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

KtTableView/KtTableView.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
E1621E1B1CBDF267003D9438 /* KtTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E1A1CBDF267003D9438 /* KtTableViewTests.m */; };
2323
E1621E261CBDF267003D9438 /* KtTableViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E251CBDF267003D9438 /* KtTableViewUITests.m */; };
2424
E1EE5C9A1CBE791E0095688B /* UIView+KtExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = E1EE5C991CBE791E0095688B /* UIView+KtExtension.m */; };
25+
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */; };
2526
/* End PBXBuildFile section */
2627

2728
/* Begin PBXContainerItemProxy section */
@@ -72,6 +73,8 @@
7273
E1621E271CBDF267003D9438 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7374
E1EE5C981CBE791E0095688B /* UIView+KtExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+KtExtension.h"; sourceTree = "<group>"; };
7475
E1EE5C991CBE791E0095688B /* UIView+KtExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+KtExtension.m"; sourceTree = "<group>"; };
76+
E1EE5C9B1CBE811C0095688B /* KtBaseTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseTableView.h; sourceTree = "<group>"; };
77+
E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseTableView.m; sourceTree = "<group>"; };
7578
/* End PBXFileReference section */
7679

7780
/* Begin PBXFrameworksBuildPhase section */
@@ -110,6 +113,8 @@
110113
E108B2AD1CBE15D800191E0B /* KtTableViewSectionObject.m */,
111114
E108B2B51CBE1F6500191E0B /* KtTableViewBaseItem.h */,
112115
E108B2B61CBE1F6500191E0B /* KtTableViewBaseItem.m */,
116+
E1EE5C9B1CBE811C0095688B /* KtBaseTableView.h */,
117+
E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */,
113118
);
114119
name = KtBaseTableViewController;
115120
sourceTree = "<group>";
@@ -327,6 +332,7 @@
327332
E1EE5C9A1CBE791E0095688B /* UIView+KtExtension.m in Sources */,
328333
E108B2B11CBE191300191E0B /* KtBaseTableViewCell.m in Sources */,
329334
E1621E081CBDF267003D9438 /* KTMainViewController.m in Sources */,
335+
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */,
330336
E1621E051CBDF267003D9438 /* AppDelegate.m in Sources */,
331337
E108B2AE1CBE15D800191E0B /* KtTableViewSectionObject.m in Sources */,
332338
E1621E021CBDF265003D9438 /* main.m in Sources */,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// KtBaseTableView.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import "KtTableViewDataSource.h"
11+
12+
@class KtTableViewSectionObject;
13+
@protocol KtTableViewDelegate<UITableViewDelegate>
14+
15+
@optional
16+
17+
/**
18+
* 选择一个cell的回调,并返回被选择cell的数据结构和indexPath
19+
*/
20+
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath;
21+
22+
- (UIView *)headerViewForSectionObject:(KtTableViewSectionObject *)sectionObject atSection:(NSInteger)section;
23+
24+
// 将来可以有 cell 的编辑,交换,左滑等回调
25+
26+
// 这个协议继承了UITableViewDelegate ,所以自己做一层中转,VC 依然需要实现某些代理方法。
27+
28+
@end
29+
30+
@interface KtBaseTableView : UITableView<UITableViewDelegate>
31+
32+
@property (nonatomic, assign) id<KtTableViewDataSource> ktDataSource;
33+
34+
@property (nonatomic, assign) id<KtTableViewDelegate> ktDelegate;
35+
36+
@end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// KtBaseTableView.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/4/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseTableView.h"
10+
#import "KtBaseTableViewCell.h"
11+
#import "KtTableViewSectionObject.h"
12+
13+
@implementation KtBaseTableView
14+
15+
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
16+
self = [super initWithFrame:frame style:style];
17+
if (self) {
18+
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
19+
self.separatorColor = [UIColor clearColor];
20+
self.showsVerticalScrollIndicator = YES;
21+
self.showsHorizontalScrollIndicator = NO;
22+
self.sectionHeaderHeight = 0;
23+
self.sectionFooterHeight = 0;
24+
self.delegate = self;
25+
}
26+
return self;
27+
}
28+
29+
- (void)setKtDataSource:(id<KtTableViewDataSource>)ktDataSource {
30+
if (_ktDataSource != ktDataSource) {
31+
_ktDataSource = ktDataSource;
32+
self.dataSource = ktDataSource;
33+
}
34+
}
35+
36+
#pragma mark - UITableViewDelegate
37+
38+
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
39+
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
40+
41+
KtTableViewBaseItem *object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
42+
Class cls = [dataSource tableView:tableView cellClassForObject:object];
43+
44+
return [cls tableView:tableView rowHeightForObject:object];
45+
}
46+
47+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
48+
if ([self.ktDelegate respondsToSelector:@selector(didSelectObject:atIndexPath:)]) {
49+
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
50+
id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
51+
[self.ktDelegate didSelectObject:object atIndexPath:indexPath];
52+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
53+
}
54+
else if ([self.ktDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
55+
[self.ktDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
56+
}
57+
}
58+
59+
60+
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
61+
if ([self.ktDelegate respondsToSelector:@selector(headerViewForSectionObject:atSection:)]) {
62+
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
63+
KtTableViewSectionObject *sectionObject = [((KtTableViewDataSource *)dataSource).sections objectAtIndex:section];
64+
65+
return [self.ktDelegate headerViewForSectionObject:sectionObject atSection:section];
66+
}
67+
else if ([self.ktDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
68+
return [self.ktDelegate tableView:tableView viewForHeaderInSection:section];
69+
}
70+
return nil;
71+
}
72+
73+
#pragma mark - 传递原生协议
74+
75+
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
76+
if ([self.ktDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)]) {
77+
[self.ktDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
78+
}
79+
}
80+
@end

0 commit comments

Comments
 (0)