-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUDTableView.m
More file actions
executable file
·328 lines (236 loc) · 12.3 KB
/
UDTableView.m
File metadata and controls
executable file
·328 lines (236 loc) · 12.3 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// UDTableView.m
//
// Copyright (c) 2012 Rolandas Razma <rolandas@razma.lt>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "UDTableView.h"
@interface UDTableView (UDPrivate)
- (void)ud_setAllowsMultipleSelectionDuringEditing:(BOOL)allowsMultipleSelectionDuringEditing;
- (BOOL)ud_allowsMultipleSelectionDuringEditing;
- (void)ud_setAllowsMultipleSelection:(BOOL)allowsMultipleSelection;
- (BOOL)ud_allowsMultipleSelection;
@end
@implementation UDTableView {
BOOL _allowsMultipleSelectionDuringEditing;
BOOL _allowsMultipleSelection;
BOOL _needsMultipleSelectionBackport;
NSMutableSet *_indexPathsForSelectedRows;
NSObject <UITableViewDataSource>*_realDataSource;
NSObject <UITableViewDelegate>*_realDelegate;
}
#pragma mark -
#pragma mark NSObject
- (void)dealloc {
[_indexPathsForSelectedRows release];
[super dealloc];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if( (self = [super initWithCoder:aDecoder]) ){
[self setAllowsMultipleSelectionDuringEditing: [aDecoder decodeBoolForKey:@"UIAllowsMultipleSelectionDuringEditing"]];
[self setAllowsMultipleSelection: [aDecoder decodeBoolForKey:@"UIAllowsMultipleSelection"]];
}
return self;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
if ( [_realDataSource respondsToSelector:aSelector] ){
return _realDataSource;
}else if ( [_realDelegate respondsToSelector:aSelector] ){
return _realDelegate;
}
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSString *aSelectorString = NSStringFromSelector(aSelector);
if( [aSelectorString isEqualToString:@"setAllowsMultipleSelectionDuringEditing:"] ){
return [[self class] instanceMethodSignatureForSelector:@selector(ud_setAllowsMultipleSelectionDuringEditing:)];
}else if( [aSelectorString isEqualToString:@"allowsMultipleSelectionDuringEditing"] ){
return [[self class] instanceMethodSignatureForSelector:@selector(ud_allowsMultipleSelectionDuringEditing)];
}else if( [aSelectorString isEqualToString:@"setAllowsMultipleSelection:"] ){
return [[self class] instanceMethodSignatureForSelector:@selector(ud_setAllowsMultipleSelection:)];
}else if( [aSelectorString isEqualToString:@"allowsMultipleSelection"] ){
return [[self class] instanceMethodSignatureForSelector:@selector(ud_allowsMultipleSelection)];
}
return nil;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
NSString *aSelectorString = NSStringFromSelector([invocation selector]);
if( [aSelectorString isEqualToString:@"setAllowsMultipleSelectionDuringEditing:"] ){
[invocation setSelector:@selector(ud_setAllowsMultipleSelectionDuringEditing:)];
[invocation invokeWithTarget:self];
}else if( [aSelectorString isEqualToString:@"allowsMultipleSelectionDuringEditing"] ){
[invocation setSelector:@selector(ud_allowsMultipleSelectionDuringEditing)];
[invocation invokeWithTarget:self];
}else if( [aSelectorString isEqualToString:@"setAllowsMultipleSelection:"] ){
[invocation setSelector:@selector(ud_setAllowsMultipleSelection:)];
[invocation invokeWithTarget:self];
}else if( [aSelectorString isEqualToString:@"allowsMultipleSelection"] ){
[invocation setSelector:@selector(ud_allowsMultipleSelection)];
[invocation invokeWithTarget:self];
}else{
[self doesNotRecognizeSelector:[invocation selector]];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector {
return ( [super respondsToSelector:aSelector] || [_realDataSource respondsToSelector:aSelector] || [_realDelegate respondsToSelector:aSelector] );
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol {
return ( [super conformsToProtocol:aProtocol] || [_realDataSource conformsToProtocol:aProtocol] || [_realDelegate conformsToProtocol:aProtocol] );
}
#pragma mark -
#pragma mark UITableView
- (void)setDataSource:(id<UITableViewDataSource>)dataSource {
_realDataSource = dataSource;
if( _needsMultipleSelectionBackport ){
[super setDataSource:(id<UITableViewDataSource>)self];
}else{
[super setDataSource:dataSource];
}
}
- (void)setDelegate:(id<UITableViewDelegate>)delegate {
_realDelegate = delegate;
if( _needsMultipleSelectionBackport ){
[super setDelegate:(id<UITableViewDelegate>)self];
}else{
[super setDelegate:delegate];
}
}
- (void)reloadData {
for( NSIndexPath *indexPath in [_indexPathsForSelectedRows allObjects] ){
[self deselectRowAtIndexPath:indexPath animated:NO];
}
[_indexPathsForSelectedRows removeAllObjects];
[super reloadData];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
for( NSIndexPath *indexPath in [_indexPathsForSelectedRows allObjects] ){
[self deselectRowAtIndexPath:indexPath animated:NO];
}
[_indexPathsForSelectedRows removeAllObjects];
[super setEditing:editing animated:animated];
}
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition {
if( _needsMultipleSelectionBackport && indexPath && ((_allowsMultipleSelectionDuringEditing && self.isEditing) || (_allowsMultipleSelection && !self.isEditing)) ){
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"tselectRowAtIndexPath:animated:scrollPosition: shouldn't be called because iOS5+ natively supports multiselect");
[_indexPathsForSelectedRows addObject:indexPath];
[[self cellForRowAtIndexPath:indexPath] setSelected:YES animated:animated];
}else{
[super selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
}
}
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
if( _needsMultipleSelectionBackport && indexPath && ((_allowsMultipleSelectionDuringEditing && self.isEditing) || (_allowsMultipleSelection && !self.isEditing)) ){
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"deselectRowAtIndexPath:animated: shouldn't be called because iOS5+ natively supports multiselect");
[_indexPathsForSelectedRows removeObject:indexPath];
[[self cellForRowAtIndexPath:indexPath] setSelected:NO animated:animated];
}else {
[super deselectRowAtIndexPath:indexPath animated:animated];
}
}
- (void)setAllowsSelection:(BOOL)allowsSelection {
[super setAllowsSelection:allowsSelection];
if( _needsMultipleSelectionBackport && ![self allowsSelection] ){
[self setAllowsMultipleSelection:NO];
}
}
- (void)setAllowsSelectionDuringEditing:(BOOL)allowsSelectionDuringEditing {
[super setAllowsSelectionDuringEditing:allowsSelectionDuringEditing];
if( _needsMultipleSelectionBackport && ![self allowsSelectionDuringEditing] ){
[self setAllowsMultipleSelectionDuringEditing:NO];
}
}
#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"tableView:cellForRowAtIndexPath: shouldn't be called because iOS5+ natively supports multiselect");
if( [_indexPathsForSelectedRows containsObject:indexPath] ){
[cell setSelected:YES];
}
if ( [_realDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)] ){
[_realDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"tableView:willSelectRowAtIndexPath: shouldn't be called because iOS5+ natively supports multiselect");
if( [_indexPathsForSelectedRows containsObject:indexPath] ){
[self deselectRowAtIndexPath:indexPath animated:NO];
if( [_realDelegate respondsToSelector:@selector(tableView:didDeselectRowAtIndexPath:)] ){
[_realDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
}
}else{
[self selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
if( [_realDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)] ){
[_realDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
return nil;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"tableView:willDeselectRowAtIndexPath: shouldn't be called because iOS5+ natively supports multiselect");
return nil;
}
#pragma mark -
#pragma mark UDTableView
- (void)ud_setAllowsMultipleSelectionDuringEditing:(BOOL)allowsMultipleSelectionDuringEditing {
if( _allowsMultipleSelectionDuringEditing == allowsMultipleSelectionDuringEditing ) return;
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"ud_setAllowsMultipleSelectionDuringEditing: shouldn't be called because iOS5+ natively supports multiselect");
_allowsMultipleSelectionDuringEditing = _needsMultipleSelectionBackport = allowsMultipleSelectionDuringEditing;
if( _allowsMultipleSelectionDuringEditing ){
[_indexPathsForSelectedRows release];
_indexPathsForSelectedRows = [[NSMutableSet alloc] init];
if( super.dataSource ) [super setDataSource:(id<UITableViewDataSource>)self];
if( super.delegate ) [super setDelegate:(id<UITableViewDelegate>)self];
[self setAllowsSelectionDuringEditing:YES];
}else{
[_indexPathsForSelectedRows release], _indexPathsForSelectedRows = nil;
[self setDelegate:_realDelegate];
[self setDataSource:_realDataSource];
}
}
- (BOOL)ud_allowsMultipleSelectionDuringEditing {
return _allowsMultipleSelectionDuringEditing;
}
- (void)ud_setAllowsMultipleSelection:(BOOL)allowsMultipleSelection {
if( _allowsMultipleSelection == allowsMultipleSelection ) return;
NSAssert(( &UIApplicationLaunchOptionsNewsstandDownloadsKey == NULL ), @"ud_setAllowsMultipleSelectionDuringEditing: shouldn't be called because iOS5+ natively supports multiselect");
_allowsMultipleSelection = _needsMultipleSelectionBackport = allowsMultipleSelection;
if( _allowsMultipleSelection ){
[_indexPathsForSelectedRows release];
_indexPathsForSelectedRows = [[NSMutableSet alloc] init];
if( super.dataSource ) [super setDataSource:(id<UITableViewDataSource>)self];
if( super.delegate ) [super setDelegate:(id<UITableViewDelegate>)self];
[self setAllowsSelection:YES];
}else{
[_indexPathsForSelectedRows release], _indexPathsForSelectedRows = nil;
[self setDelegate:_realDelegate];
[self setDataSource:_realDataSource];
}
}
- (BOOL)ud_allowsMultipleSelection {
return _allowsMultipleSelection;
}
- (NSArray *)indexPathsForSelectedRows {
if( _needsMultipleSelectionBackport ){
return [_indexPathsForSelectedRows allObjects];
}else{
return [super indexPathsForSelectedRows];
}
}
@dynamic allowsMultipleSelectionDuringEditing, allowsMultipleSelection;
@end