-
Notifications
You must be signed in to change notification settings - Fork 16
/
UITableView.m
346 lines (262 loc) · 9.45 KB
/
UITableView.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// UITableView.m
// UIKit
//
// Created by Shaun Harrison on 4/30/09.
// Copyright 2009 enormego. All rights reserved.
//
#import <UIKit/UITableView.h>
#import "UIColor.h"
#import "UITableViewCell-Private.h"
static inline CGRect CGRectFromOffsetHeight(float offset, float height) {
return CGRectMake(0.0f, offset, 100.0f, height);
}
@interface UITableView (Private)
- (void)queueReusableCell:(UITableViewCell*)aTableViewCell;
- (void)layoutVisibleCells;
- (void)removeInvisibleCells;
- (void)clearAllCells;
@end
@implementation UITableView
@synthesize style=_style, dataSource=_dataSource, delegate=_delegate;
@synthesize rowHeight=_rowHeight, sectionHeaderHeight=_sectionHeaderHeight, sectionFooterHeight=_sectionFooterHeight;
@synthesize editing=_isEditing, allowsSelection=_allowsSelection, allowsSelectionDuringEditing=_allowsSelectionDuringEditing;
@synthesize separatorStyle=_separatorStyle, separatorColor=_separatorColor;
@synthesize tableHeaderView=_tableHeaderView, tableFooterView=_tableFooterView;
- (void)setupTableViewDefaults {
_rowHeight = 44.0f;
_sectionHeaderHeight = 22.0f;
_sectionFooterHeight = 22.0f;
_allowsSelection = YES;
_allowsSelectionDuringEditing = NO;
_separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_separatorColor = [[UIColor grayColor] retain];
_reusableTableCells = [[NSMutableDictionary alloc] init];
_visibleCells = [[NSMutableArray alloc] init];
_sectionData = [[NSMutableArray alloc] init];
self.hasVerticalScroller = YES;
self.hasHorizontalScroller = NO;
self.autohidesScrollers = NO;
((UIView*)self.documentView).autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
}
- (id)initWithFrame:(NSRect)frameRect {
if((self = [super initWithFrame:frameRect])) {
[self setupTableViewDefaults];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if((self = [super initWithCoder:aDecoder])) {
[self setupTableViewDefaults];
}
return self;
}
#pragma mark Todo
- (void)reloadData {
float height = 0.0f;
NSInteger sections = 1;
BOOL variableRowHeights = [self.delegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)];
if([self.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
sections = [self.dataSource numberOfSectionsInTableView:self];
}
[_sectionData removeAllObjects];
for(int section = 0; section < sections; section++) {
NSInteger rows = [self.dataSource tableView:self numberOfRowsInSection:section];
NSMutableArray* rowInfo = [[NSMutableArray alloc] initWithCapacity:rows];
float offsetY = height;
for(int row = 0; row < rows; row++) {
float rowHeight = self.rowHeight;
if(variableRowHeights) {
rowHeight = [self.delegate tableView:self heightForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
}
NSRect rowRect = NSMakeRect(0.0f, height, self.contentView.frame.size.width, rowHeight);
[rowInfo addObject:[NSValue valueWithRect:rowRect]];
height += rowHeight;
}
NSMutableDictionary* sectionInfo = [[NSMutableDictionary alloc] initWithCapacity:2];
[sectionInfo setObject:rowInfo forKey:@"rows"];
[rowInfo release];
NSRect sectionRect = NSMakeRect(0.0f, offsetY, self.contentView.frame.size.width, height-offsetY);
[sectionInfo setObject:[NSValue valueWithRect:sectionRect] forKey:@"rect"];
[_sectionData addObject:sectionInfo];
[sectionInfo release];
}
if(height == 0) height = 1.0f;
self.documentSize = NSMakeSize([self contentSize].width, height);
[self clearAllCells];
[self layoutVisibleCells];
}
- (NSInteger)numberOfSections {
if([_dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
return [_dataSource numberOfSectionsInTableView:self];
} else {
return 1;
}
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
return [_dataSource tableView:self numberOfRowsInSection:section];
}
- (UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath {
return [_dataSource tableView:self cellForRowAtIndexPath:indexPath];
}
- (NSArray*)visibleCells {
return _visibleCells;
}
- (NSArray*)indexPathsForVisibleRows {
// ToDo: indexPathsForVisibleRows
return nil;
}
- (void)layoutVisibleCells {
CGRect clipViewBounds = NSRectToCGRect(self.contentView.bounds);
NSArray* subviews = [[[self.documentView subviews] copy] autorelease];
NSInteger section = 0;
for(NSDictionary* sectionInfo in _sectionData) {
CGRect sectionRect = NSRectToCGRect([[sectionInfo objectForKey:@"rect"] rectValue]);
sectionRect.origin.x = 0.0f;
sectionRect.size.width = self.contentView.frame.size.width;
if(!CGRectIntersectsRect(clipViewBounds, sectionRect)) {
section++;
continue;
}
NSInteger row = 0;
for(NSValue* rowRectValue in [sectionInfo objectForKey:@"rows"]) {
NSRect rowRect = [rowRectValue rectValue];
rowRect.origin.x = 0.0f;
rowRect.size.width = self.contentView.frame.size.width;
if(!CGRectIntersectsRect(clipViewBounds, NSRectToCGRect(rowRect))) {
row++;
continue;
}
BOOL skip = NO;
for(UIView* view in subviews) {
if(![view isKindOfClass:[UITableViewCell class]]) continue;
if(CGRectIntersectsRect(NSRectToCGRect(rowRect), NSRectToCGRect(view.frame))) {
skip = YES;
break;
}
}
if(skip) {
row++;
continue;
}
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* aCell = [self.dataSource tableView:self cellForRowAtIndexPath:indexPath];
// if(!aCell.separatorColor) aCell.separatorColor = _separatorColor;
aCell.frame = rowRect;
[aCell setNeedsLayout];
[aCell setNeedsDisplay];
[self.documentView addSubview:aCell];
[aCell layoutIfNeeded];
[_visibleCells addObject:aCell];
row++;
}
section++;
}
}
- (void)clearAllCells {
[_visibleCells removeAllObjects];
NSMutableSet* cells = [[NSMutableSet alloc] init];
for(UITableViewCell* cell in [self.documentView subviews]) {
if(![cell isKindOfClass:[UITableViewCell class]]) continue;
[cells addObject:cell];
}
for(UITableViewCell* cell in cells) {
[self queueReusableCell:cell];
[cell removeFromSuperview];
}
[cells release];
}
- (void)removeInvisibleCells {
NSMutableSet* cellsToRemove = [NSMutableSet set];
CGRect clipViewBounds = NSRectToCGRect(self.contentView.bounds);
for(UITableViewCell* cell in [self.documentView subviews]) {
if(![cell isKindOfClass:[UITableViewCell class]]) continue;
if(CGRectIntersectsRect(clipViewBounds, NSRectToCGRect(cell.frame))) continue;
[cellsToRemove addObject:cell];
}
[cellsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_visibleCells removeObjectsInArray:[cellsToRemove allObjects]];
for(UITableViewCell* cell in cellsToRemove) {
[self queueReusableCell:cell];
}
}
- (void)reflectScrolledClipView:(NSClipView *)aClipView{
[super reflectScrolledClipView:aClipView];
[self removeInvisibleCells];
[self layoutVisibleCells];
}
#pragma mark -
#pragma mark Live resizing methods
- (void)viewWillStartLiveResize {
[super viewWillStartLiveResize];
liveResizeScrollOffset = self.documentOffset;
}
- (void)viewDidEndLiveResize {
[super viewDidEndLiveResize];
[self removeInvisibleCells];
[self layoutVisibleCells];
}
- (void)setFrame:(NSRect)frameRect {
[super setFrame:frameRect];
if([self inLiveResize]) {
self.documentOffset = liveResizeScrollOffset;
[self removeInvisibleCells];
[self layoutVisibleCells];
}
}
#pragma mark Todo
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
}
#pragma mark Todo
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
}
#pragma mark Todo
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
}
#pragma mark Todo
- (NSIndexPath *)indexPathForSelectedRow {
return nil;
}
#pragma mark Todo
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition {
}
#pragma mark Todo
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
}
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier {
if(!identifier) return nil;
UITableViewCell* aTableViewCell = [[_reusableTableCells objectForKey:identifier] anyObject];
if(!aTableViewCell) return nil;
[aTableViewCell retain];
[[_reusableTableCells objectForKey:identifier] removeObject:aTableViewCell];
return [aTableViewCell autorelease];
}
- (void)queueReusableCell:(UITableViewCell*)aTableViewCell {
if(!aTableViewCell) return;
if(!aTableViewCell.reuseIdentifier) return;
if([[_reusableTableCells objectForKey:aTableViewCell.reuseIdentifier] containsObject:aTableViewCell]) return;
[aTableViewCell prepareForReuse];
if(![_reusableTableCells objectForKey:aTableViewCell.reuseIdentifier]) {
[_reusableTableCells setObject:[NSMutableSet setWithCapacity:1] forKey:aTableViewCell.reuseIdentifier];
}
[[_reusableTableCells objectForKey:aTableViewCell.reuseIdentifier] addObject:aTableViewCell];
}
- (void)dealloc {
[_sectionData release];
[_visibleCells release];
[_reusableTableCells release];
[super dealloc];
}
@end
@implementation NSIndexPath (UITableView)
+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section {
NSUInteger indexArr[] = {section,row};
return [NSIndexPath indexPathWithIndexes:indexArr length:2];
}
- (NSUInteger)section {
return [self indexAtPosition:0];
}
- (NSUInteger)row {
return [self indexAtPosition:1];
}
@end