/
SectionProviderTableViewDelegate.m
289 lines (226 loc) · 9.27 KB
/
SectionProviderTableViewDelegate.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// SectionProviderTableViewDelegate.m
// Cue
//
// Created by Aaron Sarazan on 10/28/11.
// Copyright (c) 2011 Cue, Inc. All rights reserved.
//
#import "SectionProviderTableViewDelegate.h"
const CGFloat kCueTableViewHeaderHeight = 36.0f;
const CGFloat kCueTableViewFooterHeight = 36.0f;
@implementation UITableView (Cue)
- (CGFloat)cueHeightForHeaderInSection:(NSInteger)section;
{
id<UITableViewDelegate> delegate = self.delegate;
id<UITableViewDataSource> dataSource = self.dataSource;
BOOL show = NO;
if ([dataSource respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
show |= !![dataSource tableView:self titleForHeaderInSection:section];
}
if (!show && [delegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
show |= !![delegate tableView:self viewForHeaderInSection:section];
}
return show ? kCueTableViewHeaderHeight : 0.0f;
}
- (CGFloat)cueHeightForFooterInSection:(NSInteger)section;
{
id<UITableViewDelegate> delegate = self.delegate;
id<UITableViewDataSource> dataSource = self.dataSource;
BOOL show = NO;
if ([dataSource respondsToSelector:@selector(tableView:titleForFooterInSection:)]) {
show |= !![dataSource tableView:self titleForFooterInSection:section];
}
if ([delegate respondsToSelector:@selector(tableView:viewForFooterInSection:)]) {
show |= !![delegate tableView:self viewForFooterInSection:section];
}
return show ? kCueTableViewFooterHeight : 0.0f;
}
@end
@implementation SectionProviderTableViewDelegate
+ (id)delegateWithProviders:(NSArray *)providers;
{
return [[self alloc] initWithProviders:providers];
}
- (id)initWithProviders:(NSArray *)providers;
{
self = [super init];
if (self) {
self.providers = providers;
_scrollsToTop = YES;
// Set the section index for all of our providers
int i = 0;
for (id<TableViewSectionProvider> provider in self.providers) {
[provider setSection:i++];
}
}
return self;
}
- (id<TableViewSectionProvider>)providerForSection:(NSInteger)section;
{
return [self.providers objectAtIndex:section];
}
- (void)update;
{
for (id provider in self.providers) {
[provider update];
}
}
#pragma mark - Scroll View
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(CGPoint *)targetContentOffset;
{
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return [self.providers count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [[self.providers objectAtIndex:section] numberOfRowsInTableView:tableView];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSInteger row = indexPath.row;
NSInteger section = indexPath.section;
id<TableViewSectionProvider> provider = [self.providers objectAtIndex:section];
if ([provider respondsToSelector:@selector(tableView:updateCell:row:)]) {
NSString * identifier = @"Cell";
if ([provider respondsToSelector:@selector(reuseIdentifierForCellAtRow:)]) {
identifier = [provider reuseIdentifierForCellAtRow:row];
}
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
if ([provider respondsToSelector:@selector(tableView:initializeCellAtRow:)]) {
cell = [provider tableView:tableView initializeCellAtRow:row];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
}
cell = [provider tableView:tableView updateCell:cell row:row];
return cell;
}
return [provider tableView:tableView cellForRow:row];
}
#pragma clang diagnostic pop
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(titleForHeaderInTableView:)]) {
return [[self.providers objectAtIndex:section] titleForHeaderInTableView:tableView];
}
return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
{
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(titleForFooterInTableView:)]) {
return [[self.providers objectAtIndex:section] titleForFooterInTableView:tableView];
}
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
{
id first = self.providers.count > 0 ? self.providers[0] : nil;
if (![first respondsToSelector:@selector(sectionIndexTitleForTableView:)]) {
return nil;
}
if (!_showSectionIndex) {
return nil;
}
NSMutableArray * retval = [NSMutableArray array];
if (self.showMagnifierSectionIndex) {
[retval addObject:UITableViewIndexSearch];
}
if (self.showFavoritesSectionIndex) {
[retval addObject:@"\u2605"];
}
for (id<TableViewSectionProvider> provider in self.providers) {
[retval addObject:[provider sectionIndexTitleForTableView:tableView]];
}
return retval;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
{
if (self.showMagnifierSectionIndex) {
index--;
}
if (self.showFavoritesSectionIndex) {
index--;
}
if (index < 0) {
CGRect frame;
if (index == -2 || (index == -1 && !self.showFavoritesSectionIndex)) { // magnifier
frame = self.searchBar.frame;
} else if (index == -1) { // favorites
frame = self.searchBar.frame;
frame.origin.y += frame.size.height;
frame.size.height = tableView.frame.size.height;
}
[tableView scrollRectToVisible:frame animated:NO];
}
return index;
}
#pragma mark - Table view delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if ([[self.providers objectAtIndex:indexPath.section] respondsToSelector:@selector(tableView:editingStyleForRow:)]) {
return [[self.providers objectAtIndex:indexPath.section] tableView:tableView editingStyleForRow:indexPath.row];
}
return UITableViewCellEditingStyleNone;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(heightForHeaderInTableView:)]) {
return [[self.providers objectAtIndex:section] heightForHeaderInTableView:tableView];
}
return [tableView cueHeightForHeaderInSection:section];
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(viewForHeaderInTableView:)]) {
return [[self.providers objectAtIndex:section] viewForHeaderInTableView:tableView];
}
return [tableView headerViewForSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(heightForFooterInTableView:)]) {
return [[self.providers objectAtIndex:section] heightForFooterInTableView:tableView];
}
return [tableView cueHeightForFooterInSection:section];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if ([[self.providers objectAtIndex:section] respondsToSelector:@selector(viewForFooterInTableView:)]) {
return [[self.providers objectAtIndex:section] viewForFooterInTableView:tableView];
}
return [tableView footerViewForSection:section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[self.providers objectAtIndex:indexPath.section] respondsToSelector:@selector(tableView:didSelectCellAtRow:)]) {
@try {
[[self.providers objectAtIndex:indexPath.section] tableView:tableView didSelectCellAtRow:indexPath.row];
} @catch (NSException * e) {
NSLog(@"%@", e);
}
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
{
if ([[self.providers objectAtIndex:indexPath.section] respondsToSelector:@selector(tableView:accessoryButtonTappedForRow:)]) {
[[self.providers objectAtIndex:indexPath.section] tableView:tableView accessoryButtonTappedForRow:indexPath.row];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if ([[self.providers objectAtIndex:indexPath.section] respondsToSelector:@selector(tableView:heightForRow:)]) {
return [[self.providers objectAtIndex:indexPath.section] tableView:tableView heightForRow:indexPath.row];
}
return 44.0f;
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
{
return !scrollView.hidden;
}
@end