forked from bestswifter/MySampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKtBaseTableView.m
More file actions
133 lines (114 loc) · 4.81 KB
/
KtBaseTableView.m
File metadata and controls
133 lines (114 loc) · 4.81 KB
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
//
// KtBaseTableView.m
// KtTableView
//
// Created by baidu on 16/4/13.
// Copyright © 2016年 zxy. All rights reserved.
//
#import "KtBaseTableView.h"
#import "KtBaseTableViewCell.h"
#import "KtTableViewSectionObject.h"
#import "KtTableViewBaseItem.h"
#import "MJRefresh.h"
@implementation KtBaseTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
self = [super initWithFrame:frame style:style];
if (self) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.separatorColor = [UIColor clearColor];
self.showsVerticalScrollIndicator = YES;
self.showsHorizontalScrollIndicator = NO;
self.sectionHeaderHeight = 0;
self.sectionFooterHeight = 0;
self.delegate = self;
self.isNeedPullDownToRefreshAction = NO;
self.isNeedPullUpToRefreshAction = NO;
}
return self;
}
- (void)setKtDataSource:(id<KtTableViewDataSource>)ktDataSource {
if (_ktDataSource != ktDataSource) {
_ktDataSource = ktDataSource;
self.dataSource = ktDataSource;
}
}
#pragma mark - 上拉加载和下拉刷新
- (void)setIsNeedPullDownToRefreshAction:(BOOL)isEnable {
if (_isNeedPullDownToRefreshAction == isEnable) {
return;
}
_isNeedPullDownToRefreshAction = isEnable;
__block typeof(self) weakSelf = self;
if (_isNeedPullDownToRefreshAction) {
self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
if ([weakSelf.ktDelegate respondsToSelector:@selector(pullDownToRefreshAction)]) {
[weakSelf.ktDelegate pullDownToRefreshAction];
}
}];
}
}
- (void)setIsNeedPullUpToRefreshAction:(BOOL)isEnable
{
if (_isNeedPullUpToRefreshAction == isEnable) {
return;
}
_isNeedPullUpToRefreshAction = isEnable;
__block typeof(self) weakSelf = self;
if (_isNeedPullUpToRefreshAction) {
self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
if ([weakSelf.ktDelegate respondsToSelector:@selector(pullUpToRefreshAction)]) {
[weakSelf.ktDelegate pullUpToRefreshAction];
}
}];
}
}
- (void)stopRefreshingAnimation {
if ([self.mj_header isRefreshing]) {
[self.mj_header endRefreshing];
}
if ([self.mj_footer isRefreshing]) {
[self.mj_footer endRefreshing];
}
}
- (void)triggerRefreshing {
[self.mj_header beginRefreshing];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
KtTableViewBaseItem *object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
Class cls = [dataSource tableView:tableView cellClassForObject:object];
if (object.cellHeight == CellInvalidHeight) { // 没有高度缓存
object.cellHeight = [cls tableView:tableView rowHeightForObject:object];
}
return object.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.ktDelegate respondsToSelector:@selector(didSelectObject:atIndexPath:)]) {
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
id object = [dataSource tableView:tableView objectForRowAtIndexPath:indexPath];
[self.ktDelegate didSelectObject:object atIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if ([self.ktDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[self.ktDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if ([self.ktDelegate respondsToSelector:@selector(headerViewForSectionObject:atSection:)]) {
id<KtTableViewDataSource> dataSource = (id<KtTableViewDataSource>)tableView.dataSource;
KtTableViewSectionObject *sectionObject = [((KtTableViewDataSource *)dataSource).sections objectAtIndex:section];
return [self.ktDelegate headerViewForSectionObject:sectionObject atSection:section];
}
else if ([self.ktDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
return [self.ktDelegate tableView:tableView viewForHeaderInSection:section];
}
return nil;
}
#pragma mark - 传递原生协议
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.ktDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)]) {
[self.ktDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
}
}
@end