forked from soffes/sstoolkit
/
SSCollectionView.m
841 lines (626 loc) · 26.8 KB
/
SSCollectionView.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//
// SSCollectionView.m
// SSToolkit
//
// Created by Sam Soffes on 6/11/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//
#import "SSCollectionView.h"
#import "SSCollectionViewInternal.h"
#import "SSCollectionViewItem.h"
#import "SSCollectionViewItemInternal.h"
#import "SSCollectionViewItemTableViewCell.h"
#import "SSCollectionViewExtremityTableViewCell.h"
#import "SSDrawingUtilities.h"
#import "UIView+SSToolkitAdditions.h"
typedef enum {
SSCollectionViewCellTypeRow,
SSCollectionViewCellTypeHeader,
SSCollectionViewCellTypeFooter
} SSCollectionViewCellType;
static NSString *kSSCollectionViewSectionNumberOfItemsKey = @"SSCollectionViewSectionNumberOfItems";
static NSString *kSSCollectionViewSectionNumberOfRowsKey = @"SSCollectionViewSectionNumberOfRows";
static NSString *kSSCollectionViewSectionNumberOfItemsPerRowsKey = @"SSCollectionViewSectionNumberOfItemsPerRows";
static NSString *kSSCollectionViewSectionHeaderViewKey = @"SSCollectionViewSectionHeaderView";
static NSString *kSSCollectionViewSectionFooterViewKey = @"SSCollectionViewSectionFooterView";
static NSString *kSSCollectionViewSectionHeaderHeightKey = @"SSCollectionViewSectionHeaderHeight";
static NSString *kSSCollectionViewSectionFooterHeightKey = @"SSCollectionViewSectionFooterHeight";
static NSString *kSSCollectionViewSectionItemSizeKey = @"SSCollectionViewSectionItemSize";
@interface SSCollectionView (PrivateMethods)
- (void)_reuseItem:(SSCollectionViewItem *)item;
- (void)_reuseItems:(NSArray *)items;
- (CGSize)_itemSizeForSection:(NSUInteger)section;
- (NSUInteger)_numberOfItemsPerRowForSection:(NSUInteger)section;
- (NSUInteger)_numberOfRowsInSection:(NSUInteger)section;
- (NSArray *)_itemsForRowIndexPath:(NSIndexPath *)rowIndexPath;
- (NSIndexPath *)_cellIndexPathFromItemIndexPath:(NSIndexPath *)rowIndexPath;
- (SSCollectionViewCellType)_cellTypeForRowIndexPath:(NSIndexPath *)indexPath;
- (UIView *)_extremityViewForSection:(NSUInteger)section type:(SSCollectionViewCellType)type;
- (NSMutableDictionary *)_sectionInfoForIndex:(NSUInteger)section;
- (id)_sectionInfoItemForKey:(NSString *)key section:(NSUInteger)section;
- (void)_setSectionInfoItem:(id)object forKey:(NSString *)key section:(NSUInteger)section;
@end
@implementation SSCollectionView {
NSMutableSet *_visibleItems;
NSMutableDictionary *_reuseableItems;
NSMutableDictionary *_sectionCache;
UITableView *_tableView;
}
#pragma mark - Accessors
@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;
@synthesize minimumColumnSpacing = _minimumColumnSpacing;
@synthesize rowSpacing = _rowSpacing;
@synthesize allowsSelection = _allowsSelection;
@synthesize extremitiesStyle = _extremitiesStyle;
- (void)setDataSource:(id<SSCollectionViewDataSource>)dataSource {
_dataSource = dataSource;
if (_delegate && _dataSource) {
[self reloadData];
}
}
- (void)setDelegate:(id<SSCollectionViewDelegate>)delegate {
_delegate = delegate;
if (_delegate && _dataSource) {
[self reloadData];
}
}
- (UIScrollView *)scrollView {
return _tableView;
}
- (UIView *)backgroundView {
return _tableView.backgroundView;
}
- (void)setBackgroundView:(UIView *)background {
_tableView.backgroundView = background;
}
- (UIView *)collectionHeaderView {
return _tableView.tableHeaderView;
}
- (void)setCollectionHeaderView:(UIView *)collectionHeaderView {
_tableView.tableHeaderView = collectionHeaderView;
}
- (UIView *)collectionFooterView {
return _tableView.tableFooterView;
}
- (void)setCollectionFooterView:(UIView *)collectionFooterView {
_tableView.tableFooterView = collectionFooterView;
}
- (void)setBackgroundColor:(UIColor *)color {
[super setBackgroundColor:color];
_tableView.backgroundColor = color;
}
- (void)setExtremitiesStyle:(SSCollectionViewExtremitiesStyle)style {
if (_extremitiesStyle == style) {
return;
}
_extremitiesStyle = style;
[self reloadData];
}
#pragma mark - NSObject
- (void)dealloc {
self.dataSource = nil;
self.delegate = nil;
[_visibleItems removeAllObjects];
[_visibleItems release];
_visibleItems = nil;
[_reuseableItems removeAllObjects];
[_reuseableItems release];
_reuseableItems = nil;
_tableView.dataSource = nil;
_tableView.delegate = nil;
[_tableView release];
[_sectionCache removeAllObjects];
[_sectionCache release];
_sectionCache = nil;
[super dealloc];
}
#pragma mark - UIView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;
_minimumColumnSpacing = 10.0f;
_rowSpacing = 20.0f;
_allowsSelection = YES;
_visibleItems = [[NSMutableSet alloc] init];
_reuseableItems = [[NSMutableDictionary alloc] init];
_sectionCache = [[NSMutableDictionary alloc] init];
_tableView = [[UITableView alloc] initWithFrame:CGRectSetZeroOrigin(frame)];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self addSubview:_tableView];
}
return self;
}
- (void)setFrame:(CGRect)frame {
BOOL shouldReload = (CGSizeEqualToSize(frame.size, self.frame.size) == NO);
[super setFrame:frame];
if (shouldReload) {
[self reloadData];
}
}
#pragma mark - SSCollectionView
- (void)reloadData {
if (![self superview]) {
return;
}
[_sectionCache removeAllObjects];
[_tableView reloadData];
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
}
}
- (SSCollectionViewItem *)dequeueReusableItemWithIdentifier:(NSString *)identifier {
if (!identifier) {
return nil;
}
NSMutableArray *items = [_reuseableItems objectForKey:identifier];
if (!items || [items count] == 0) {
return nil;
}
SSCollectionViewItem *item = [[items lastObject] retain];
[items removeObject:item];
[item prepareForReuse];
return [item autorelease];
}
- (SSCollectionViewItem *)itemPathForIndex:(NSIndexPath *)indexPath {
__block SSCollectionViewItem *item = nil;
[_visibleItems enumerateObjectsUsingBlock:^(id object, BOOL *stop) {
if ([[(SSCollectionViewItem *)object indexPath] isEqual:indexPath]) {
item = object;
*stop = YES;
}
}];
return item;
}
- (NSIndexPath *)indexPathForItem:(SSCollectionViewItem *)item {
return item.indexPath;
}
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(SSCollectionViewScrollPosition)scrollPosition {
// Notify delegate that it will select
if ([self.delegate respondsToSelector:@selector(collectionView:willSelectItemAtIndexPath:)]) {
[self.delegate collectionView:self willSelectItemAtIndexPath:indexPath];
}
// Select
SSCollectionViewItem *item = [self itemPathForIndex:indexPath];
[item setHighlighted:NO animated:NO];
[item setSelected:YES animated:YES];
// Scroll to position
if (scrollPosition == SSCollectionViewScrollPositionTop || scrollPosition == SSCollectionViewScrollPositionMiddle ||
scrollPosition == SSCollectionViewScrollPositionBottom) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
// Notify delegate that it did selection
if ([self.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
[self.delegate collectionView:self didSelectItemAtIndexPath:indexPath];
}
}
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
// Notify delegate that it will deselect
if ([self.delegate respondsToSelector:@selector(collectionView:willDeselectItemAtIndexPath:)]) {
[self.delegate collectionView:self willDeselectItemAtIndexPath:indexPath];
}
// Deselect
SSCollectionViewItem *item = [self itemPathForIndex:indexPath];
[item setHighlighted:NO animated:NO];
[item setSelected:NO animated:YES];
// Notify delegate that it did deselection
if ([self.delegate respondsToSelector:@selector(collectionView:didDeselectItemAtIndexPath:)]) {
[self.delegate collectionView:self didDeselectItemAtIndexPath:indexPath];
}
}
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(SSCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
NSIndexPath *cellIndexPath = [self _cellIndexPathFromItemIndexPath:indexPath];
[_tableView scrollToRowAtIndexPath:cellIndexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:animated];
}
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths {
NSMutableArray *rowIndexPaths = [[NSMutableArray alloc] init];
[indexPaths enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
NSIndexPath *itemIndexPath = (NSIndexPath *)object;
NSIndexPath *rowIndexPath = [self _cellIndexPathFromItemIndexPath:itemIndexPath];
if (![rowIndexPaths containsObject:rowIndexPath]) {
[rowIndexPaths addObject:rowIndexPath];
}
}];
[_tableView reloadRowsAtIndexPaths:rowIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[rowIndexPaths release];
}
- (NSUInteger)numberOfSections {
if ([_dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
return [_dataSource numberOfSectionsInCollectionView:self];
}
return 1;
}
- (NSUInteger)numberOfItemsInSection:(NSUInteger)section {
NSNumber *items = [self _sectionInfoItemForKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
if (!items) {
if ([_dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)] == NO) {
return 0;
}
items = [NSNumber numberWithUnsignedInteger:[_dataSource collectionView:self numberOfItemsInSection:section]];
[self _setSectionInfoItem:items forKey:kSSCollectionViewSectionNumberOfItemsKey section:section];
}
return [items unsignedIntegerValue];
}
- (CGRect)rectForSection:(NSUInteger)section {
return [_tableView rectForSection:(NSInteger)section];
}
- (CGRect)rectForHeaderInSection:(NSUInteger)section {
return [_tableView rectForSection:(NSInteger)section];
}
- (CGRect)rectForFooterInSection:(NSUInteger)section {
return [_tableView rectForFooterInSection:(NSInteger)section];
}
#pragma mark - Private Methods
- (void)_reuseItem:(SSCollectionViewItem *)item {
[_visibleItems removeObject:item];
if (!item.reuseIdentifier) {
NSLog(@"[SSCollectionView] Your item identifier is nil. You should really provide a reuse identifier.");
return;
}
NSMutableArray *items = [_reuseableItems objectForKey:item.reuseIdentifier];
if (!items) {
[_reuseableItems setObject:[NSMutableArray array] forKey:item.reuseIdentifier];
}
[items addObject:item];
}
- (void)_reuseItems:(NSArray *)items {
[items enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[self _reuseItem:(SSCollectionViewItem *)object];
}];
}
- (CGSize)_itemSizeForSection:(NSUInteger)section {
NSValue *size = [self _sectionInfoItemForKey:kSSCollectionViewSectionItemSizeKey section:section];
if (size) {
return [size CGSizeValue];
}
if ([_delegate respondsToSelector:@selector(collectionView:itemSizeForSection:)] == NO) {
[[NSException exceptionWithName:SSCollectionViewInvalidItemSizeExceptionName reason:SSCollectionViewInvalidItemSizeExceptionReason userInfo:nil] raise];
return CGSizeZero;
}
CGSize itemSize = [_delegate collectionView:self itemSizeForSection:section];
if (CGSizeEqualToSize(itemSize, CGSizeZero)) {
[[NSException exceptionWithName:SSCollectionViewInvalidItemSizeExceptionName reason:SSCollectionViewInvalidItemSizeExceptionReason userInfo:nil] raise];
return CGSizeZero;
}
[self _setSectionInfoItem:[NSValue valueWithCGSize:itemSize] forKey:kSSCollectionViewSectionItemSizeKey section:section];
return itemSize;
}
- (NSUInteger)_numberOfItemsPerRowForSection:(NSUInteger)section {
NSNumber *items = [self _sectionInfoItemForKey:kSSCollectionViewSectionNumberOfItemsPerRowsKey section:section];
if (items) {
return [items unsignedIntegerValue];
}
CGSize itemSize = [self _itemSizeForSection:section];
NSUInteger itemsPerRow = (NSUInteger)floorf(self.frame.size.width / (itemSize.width + _minimumColumnSpacing));
[self _setSectionInfoItem:[NSNumber numberWithUnsignedInteger:itemsPerRow] forKey:kSSCollectionViewSectionNumberOfItemsPerRowsKey section:section];
return itemsPerRow;
}
- (NSUInteger)_numberOfRowsInSection:(NSUInteger)section {
NSNumber *numberOfRows = [self _sectionInfoItemForKey:kSSCollectionViewSectionNumberOfRowsKey section:section];
if (numberOfRows) {
return [numberOfRows unsignedIntegerValue];
}
NSUInteger totalItems = [self numberOfItemsInSection:section];
NSUInteger itemsPerRow = [self _numberOfItemsPerRowForSection:section];
if (itemsPerRow == 0) {
return 0;
}
NSUInteger rows = (NSUInteger)ceilf((CGFloat)totalItems / (CGFloat)itemsPerRow);
// Check for headers and footers
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
if ([self _extremityViewForSection:section type:SSCollectionViewCellTypeHeader]) {
rows++;
}
if ([self _extremityViewForSection:section type:SSCollectionViewCellTypeFooter]) {
rows++;
}
}
[self _setSectionInfoItem:[NSNumber numberWithUnsignedInteger:rows] forKey:kSSCollectionViewSectionNumberOfRowsKey section:section];
return rows;
}
- (CGFloat)_itemSpacingForSection:(NSUInteger)section {
CGSize itemSize = [self _itemSizeForSection:section];
NSUInteger itemsPerRow = [self _numberOfItemsPerRowForSection:section];
return roundf((self.frame.size.width - (itemSize.width * (CGFloat)itemsPerRow)) / (itemsPerRow + 1));
}
- (NSArray *)_itemsForRowIndexPath:(NSIndexPath *)rowIndexPath {
NSUInteger totalItems = [self numberOfItemsInSection:rowIndexPath.section];
NSUInteger itemsPerRow = [self _numberOfItemsPerRowForSection:rowIndexPath.section];
NSUInteger row = (NSUInteger)rowIndexPath.row;
// Check for header
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling &&
[self _sectionInfoItemForKey:kSSCollectionViewSectionHeaderViewKey section:rowIndexPath.section]) {
row--;
}
NSUInteger startIndex = itemsPerRow * row;
NSUInteger endIndex = (NSUInteger)fmin(totalItems, startIndex + itemsPerRow);
NSMutableArray *items = [[[NSMutableArray alloc] initWithCapacity:endIndex - startIndex] autorelease];
for (NSUInteger i = startIndex; i < endIndex; i++) {
NSIndexPath *itemIndexPath = [NSIndexPath indexPathForRow:i inSection:rowIndexPath.section];
SSCollectionViewItem *item = [_dataSource collectionView:self itemForIndexPath:itemIndexPath];
if (item == nil) {
NSException *exception = [NSException exceptionWithName:kSSCollectionViewNilItemExceptionName
reason:kSSCollectionViewNilItemExceptionReason userInfo:nil];
[exception raise];
return nil;
}
item.tag = (NSInteger)i;
item.indexPath = itemIndexPath;
item.collectionView = self;
[_visibleItems addObject:item];
[items addObject:item];
}
return items;
}
- (NSIndexPath *)_cellIndexPathFromItemIndexPath:(NSIndexPath *)rowIndexPath {
NSUInteger itemsPerRow = [self _numberOfItemsPerRowForSection:rowIndexPath.section];
// Avoid division by zero
if (itemsPerRow == 0) {
return nil;
}
NSUInteger row = (NSUInteger)floor(rowIndexPath.row / itemsPerRow);
NSUInteger offset = 0;
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling &&
[self _sectionInfoItemForKey:kSSCollectionViewSectionHeaderViewKey section:rowIndexPath.row]) {
offset = 1;
}
return [NSIndexPath indexPathForRow:(row + offset) inSection:rowIndexPath.section];
}
- (SSCollectionViewCellType)_cellTypeForRowIndexPath:(NSIndexPath *)indexPath {
// If extremity style is fixed, everything is a row
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleFixed) {
return SSCollectionViewCellTypeRow;
}
// Check for header
if (indexPath.row == 0 && [self _sectionInfoItemForKey:kSSCollectionViewSectionHeaderViewKey section:indexPath.section]) {
return SSCollectionViewCellTypeHeader;
}
// Check for footer
BOOL hasFooter = ([self _sectionInfoItemForKey:kSSCollectionViewSectionFooterViewKey section:indexPath.section] != nil);
if (hasFooter && (NSUInteger)indexPath.row == [self _numberOfRowsInSection:indexPath.section] - 1) {
return SSCollectionViewCellTypeFooter;
}
return SSCollectionViewCellTypeRow;
}
- (UIView *)_extremityViewForSection:(NSUInteger)section type:(SSCollectionViewCellType)type {
BOOL isHeader = (type == SSCollectionViewCellTypeHeader);
NSString *key = isHeader ? kSSCollectionViewSectionHeaderViewKey : kSSCollectionViewSectionFooterViewKey;
id extremity = [self _sectionInfoItemForKey:key section:section];
// If the extremeity isn't cached, hit the delegate
if (!extremity) {
// Header
if (isHeader) {
if ([_delegate respondsToSelector:@selector(collectionView:viewForHeaderInSection:)]) {
extremity = [_delegate collectionView:self viewForHeaderInSection:section];
}
}
// Footer
else {
if ([_delegate respondsToSelector:@selector(collectionView:viewForFooterInSection:)]) {
extremity = [_delegate collectionView:self viewForFooterInSection:section];
}
}
// Handle nil
if (!extremity) {
extremity = [NSNull null];
}
// Cache
[self _setSectionInfoItem:extremity forKey:key section:section];
}
// Return nil instead of null
if (extremity == [NSNull null]) {
return nil;
}
return extremity;
}
- (NSMutableDictionary *)_sectionInfoForIndex:(NSUInteger)section {
NSNumber *sectionKey = [NSNumber numberWithUnsignedInteger:section];
NSMutableDictionary *dictionary = [_sectionCache objectForKey:sectionKey];
if (dictionary) {
return dictionary;
}
dictionary = [[NSMutableDictionary alloc] init];
[_sectionCache setObject:dictionary forKey:sectionKey];
[dictionary release];
return dictionary;
}
- (id)_sectionInfoItemForKey:(NSString *)key section:(NSUInteger)section {
NSDictionary *dictionary = [self _sectionInfoForIndex:section];
id object = [dictionary objectForKey:key];
if ([object isEqual:[NSNull null]]) {
return nil;
}
return object;
}
- (void)_setSectionInfoItem:(id)object forKey:(NSString *)key section:(NSUInteger)section {
NSMutableDictionary *dictionary = [self _sectionInfoForIndex:section];
if (!object) {
object = [NSNull null];
}
[dictionary setObject:object forKey:key];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return (NSInteger)[self numberOfSections];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (NSInteger)[self _numberOfRowsInSection:(NSUInteger)section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)rowIndexPath {
static NSString *itemCellIdentifier = @"SSCollectionViewItemTableViewCellIdentifier";
static NSString *extremityCellIdentifier = @"SSCollectionViewExtremityTableViewCellIdentifier";
SSCollectionViewCellType cellType = [self _cellTypeForRowIndexPath:rowIndexPath];
// Extremities
if (cellType != SSCollectionViewCellTypeRow) {
SSCollectionViewExtremityTableViewCell *cell = (SSCollectionViewExtremityTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:extremityCellIdentifier];
if (!cell) {
cell = [[[SSCollectionViewExtremityTableViewCell alloc] initWithReuseIdentifier:extremityCellIdentifier] autorelease];
}
cell.extrimityView = [self _extremityViewForSection:rowIndexPath.section type:cellType];
return cell;
}
// Normal row
SSCollectionViewItemTableViewCell *cell = (SSCollectionViewItemTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:itemCellIdentifier];
if (!cell) {
cell = [[[SSCollectionViewItemTableViewCell alloc] initWithReuseIdentifier:itemCellIdentifier] autorelease];
cell.collectionView = self;
}
cell.itemSize = [self _itemSizeForSection:rowIndexPath.section];
cell.itemSpacing = [self _itemSpacingForSection:rowIndexPath.section];
cell.items = [self _itemsForRowIndexPath:rowIndexPath];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)rowIndexPath {
// If scrolling extremity style, provide the height for the extermity
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
SSCollectionViewCellType cellType = [self _cellTypeForRowIndexPath:rowIndexPath];
// Header
if (cellType == SSCollectionViewCellTypeHeader) {
return [[self _sectionInfoItemForKey:kSSCollectionViewSectionHeaderHeightKey section:rowIndexPath.section] floatValue];
}
// Footer
else if (cellType == SSCollectionViewCellTypeFooter) {
return [[self _sectionInfoItemForKey:kSSCollectionViewSectionFooterHeightKey section:rowIndexPath.section] floatValue];
}
}
// Row
return [self _itemSizeForSection:rowIndexPath.section].height + _rowSpacing;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// If scrolling extremity style, don't use table view's header since we will draw it in a cell
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
// Hit delegate and cache result for use later
if ([_delegate respondsToSelector:@selector(collectionView:viewForHeaderInSection:)]) {
UIView *view = [_delegate collectionView:self viewForHeaderInSection:(NSUInteger)section];
[self _setSectionInfoItem:view forKey:kSSCollectionViewSectionHeaderViewKey section:(NSUInteger)section];
}
return nil;
}
// If the collection view's delegate provides a header, forward it to the table view
if ([_delegate respondsToSelector:@selector(collectionView:viewForHeaderInSection:)]) {
return [_delegate collectionView:self viewForHeaderInSection:(NSUInteger)section];
}
// Default to none
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// If scrolling extremity style, don't use table view's header since we will draw it in a cell
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
// Hit delegate and cache result for use later
if ([_delegate respondsToSelector:@selector(collectionView:heightForHeaderInSection:)]) {
CGFloat height = [_delegate collectionView:self heightForHeaderInSection:(NSUInteger)section];
[self _setSectionInfoItem:[NSNumber numberWithFloat:height] forKey:kSSCollectionViewSectionHeaderHeightKey section:(NSUInteger)section];
}
return 0.0f;
}
// If the collection view's delegate provides a header height, forward it to the table view
if ([_delegate respondsToSelector:@selector(collectionView:heightForHeaderInSection:)]) {
return [_delegate collectionView:self heightForHeaderInSection:(NSUInteger)section];
}
// Default to none
return 0.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
// If scrolling extremity style, don't use table view's footer since we will draw it in a cell
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
// Hit delegate and cache result for use later
if ([_delegate respondsToSelector:@selector(collectionView:viewForFooterInSection:)]) {
UIView *view = [_delegate collectionView:self viewForFooterInSection:(NSUInteger)section];
[self _setSectionInfoItem:view forKey:kSSCollectionViewSectionFooterViewKey section:(NSUInteger)section];
}
return nil;
}
// If the collection view's delegate provides a footer, forward it to the table view
if ([_delegate respondsToSelector:@selector(collectionView:viewForFooterInSection:)]) {
return [_delegate collectionView:self viewForFooterInSection:(NSUInteger)section];
}
// Default to none
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// If scrolling extremity style, don't use table view's footer since we will draw it in a cell
if (_extremitiesStyle == SSCollectionViewExtremitiesStyleScrolling) {
// Hit delegate and cache result for use later
if ([_delegate respondsToSelector:@selector(collectionView:heightForFooterInSection:)]) {
CGFloat height = [_delegate collectionView:self heightForFooterInSection:(NSUInteger)section];
[self _setSectionInfoItem:[NSNumber numberWithFloat:height] forKey:kSSCollectionViewSectionFooterHeightKey section:(NSUInteger)section];
}
return 0.0f;
}
// If the collection view's delegate provides a footer height, forward it to the table view
if ([_delegate respondsToSelector:@selector(collectionView:heightForFooterInSection:)]) {
return [_delegate collectionView:self heightForFooterInSection:(NSUInteger)section];
}
// Default to none
return 0.0f;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Make sure extremities are on top
if ([cell isKindOfClass:[SSCollectionViewExtremityTableViewCell class]]) {
// Put it under the scroll bar. I know this is awful.
NSArray *subviews = [tableView subviews];
if ([subviews count] > 2) {
[cell removeFromSuperview];
[tableView insertSubview:cell atIndex:[subviews count] - 2];
}
return;
}
// Forward delegate message for items if the delegate implements the delegate
if ([_delegate respondsToSelector:@selector(collectionView:willDisplayItem:atIndexPath:)]) {
NSArray *items = [self _itemsForRowIndexPath:indexPath];
if (!items) {
return;
}
// Send for each item
for (SSCollectionViewItem *item in items) {
[_delegate collectionView:self willDisplayItem:item atIndexPath:item.indexPath];
}
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[_delegate scrollViewDidScroll:aScrollView];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[_delegate scrollViewWillBeginDragging:aScrollView];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)aScrollView willDecelerate:(BOOL)decelerate {
if ([_delegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
[_delegate scrollViewDidEndDragging:aScrollView willDecelerate:decelerate];
}
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)]) {
[_delegate scrollViewWillBeginDecelerating:aScrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[_delegate scrollViewDidEndDecelerating:aScrollView];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)]) {
[_delegate scrollViewDidEndScrollingAnimation:aScrollView];
}
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)]) {
return [_delegate scrollViewShouldScrollToTop:aScrollView];
}
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)aScrollView {
if ([_delegate respondsToSelector:@selector(scrollViewDidScrollToTop:)]) {
[_delegate scrollViewDidScrollToTop:aScrollView];
}
}
@end