/
PSTCollectionView.m
2319 lines (1951 loc) · 110 KB
/
PSTCollectionView.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PSTCollectionView.m
// PSPDFKit
//
// Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
//
#import "PSTCollectionView.h"
#import "PSTCollectionViewData.h"
#import "PSTCollectionViewLayout+Internals.h"
#import "PSTCollectionViewItemKey.h"
#import <objc/runtime.h>
@interface PSTCollectionViewLayout (Internal)
@property (nonatomic, unsafe_unretained) PSTCollectionView *collectionView;
@end
@interface PSTCollectionViewData (Internal)
- (void)prepareToLoadData;
@end
@interface PSTCollectionViewCell (Internal)
- (void)performSelectionSegue;
@end
@interface PSTCollectionViewUpdateItem ()
- (NSIndexPath *)indexPath;
- (BOOL)isSectionOperation;
@end
@interface PSTCollectionViewLayoutAttributes () {
char junk[128];
}
@property (nonatomic, copy) NSString *elementKind;
@end
CGFloat PSTSimulatorAnimationDragCoefficient(void);
@class PSTCollectionViewExt;
@interface PSTCollectionView () <UIScrollViewDelegate> {
// ivar layout needs to EQUAL to UICollectionView.
PSTCollectionViewLayout *_layout;
__unsafe_unretained id<PSTCollectionViewDataSource> _dataSource;
UIView *_backgroundView;
NSMutableSet *_indexPathsForSelectedItems;
NSMutableDictionary *_cellReuseQueues;
NSMutableDictionary *_supplementaryViewReuseQueues;
NSMutableDictionary *_decorationViewReuseQueues;
NSMutableSet *_indexPathsForHighlightedItems;
int _reloadingSuspendedCount;
PSTCollectionReusableView *_firstResponderView;
UIView *_newContentView;
int _firstResponderViewType;
NSString *_firstResponderViewKind;
NSIndexPath *_firstResponderIndexPath;
NSMutableDictionary *_allVisibleViewsDict;
NSIndexPath *_pendingSelectionIndexPath;
NSMutableSet *_pendingDeselectionIndexPaths;
PSTCollectionViewData *_collectionViewData;
id _update;
CGRect _visibleBoundRects;
CGRect _preRotationBounds;
CGPoint _rotationBoundsOffset;
int _rotationAnimationCount;
int _updateCount;
NSMutableArray *_insertItems;
NSMutableArray *_deleteItems;
NSMutableArray *_reloadItems;
NSMutableArray *_moveItems;
NSArray *_originalInsertItems;
NSArray *_originalDeleteItems;
UITouch *_currentTouch;
void (^_updateCompletionHandler)(BOOL finished);
NSMutableDictionary *_cellClassDict;
NSMutableDictionary *_cellNibDict;
NSMutableDictionary *_supplementaryViewClassDict;
NSMutableDictionary *_supplementaryViewNibDict;
NSMutableDictionary *_cellNibExternalObjectsTables;
NSMutableDictionary *_supplementaryViewNibExternalObjectsTables;
struct {
unsigned int delegateShouldHighlightItemAtIndexPath : 1;
unsigned int delegateDidHighlightItemAtIndexPath : 1;
unsigned int delegateDidUnhighlightItemAtIndexPath : 1;
unsigned int delegateShouldSelectItemAtIndexPath : 1;
unsigned int delegateShouldDeselectItemAtIndexPath : 1;
unsigned int delegateDidSelectItemAtIndexPath : 1;
unsigned int delegateDidDeselectItemAtIndexPath : 1;
unsigned int delegateSupportsMenus : 1;
unsigned int delegateDidEndDisplayingCell : 1;
unsigned int delegateDidEndDisplayingSupplementaryView : 1;
unsigned int dataSourceNumberOfSections : 1;
unsigned int dataSourceViewForSupplementaryElement : 1;
unsigned int reloadSkippedDuringSuspension : 1;
unsigned int scheduledUpdateVisibleCells : 1;
unsigned int scheduledUpdateVisibleCellLayoutAttributes : 1;
unsigned int allowsSelection : 1;
unsigned int allowsMultipleSelection : 1;
unsigned int updating : 1;
unsigned int fadeCellsForBoundsChange : 1;
unsigned int updatingLayout : 1;
unsigned int needsReload : 1;
unsigned int reloading : 1;
unsigned int skipLayoutDuringSnapshotting : 1;
unsigned int layoutInvalidatedSinceLastCellUpdate : 1;
unsigned int doneFirstLayout : 1;
}_collectionViewFlags;
CGPoint _lastLayoutOffset;
char filler[200]; // [HACK] Our class needs to be larger than Apple's class for the superclass change to work.
}
@property (nonatomic, strong) PSTCollectionViewData *collectionViewData;
@property (nonatomic, strong, readonly) PSTCollectionViewExt *extVars;
@property (nonatomic, readonly) id currentUpdate;
@property (nonatomic, readonly) NSDictionary *visibleViewsDict;
@property (nonatomic, assign) CGRect visibleBoundRects;
@end
// Used by PSTCollectionView for external variables.
// (We need to keep the total class size equal to the UICollectionView variant)
@interface PSTCollectionViewExt : NSObject
@property (nonatomic, unsafe_unretained) id<PSTCollectionViewDelegate> collectionViewDelegate;
@property (nonatomic, strong) PSTCollectionViewLayout *nibLayout;
@property (nonatomic, strong) NSDictionary *nibCellsExternalObjects;
@property (nonatomic, strong) NSDictionary *supplementaryViewsExternalObjects;
@property (nonatomic, strong) NSIndexPath *touchingIndexPath;
@property (nonatomic, strong) NSIndexPath *currentIndexPath;
@end
@implementation PSTCollectionViewExt
@end
const char kPSTColletionViewExt;
@implementation PSTCollectionView
@synthesize collectionViewLayout = _layout;
@synthesize currentUpdate = _update;
@synthesize visibleViewsDict = _allVisibleViewsDict;
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
static void PSTCollectionViewCommonSetup(PSTCollectionView *_self) {
_self.allowsSelection = YES;
_self->_indexPathsForSelectedItems = [NSMutableSet new];
_self->_indexPathsForHighlightedItems = [NSMutableSet new];
_self->_cellReuseQueues = [NSMutableDictionary new];
_self->_supplementaryViewReuseQueues = [NSMutableDictionary new];
_self->_decorationViewReuseQueues = [NSMutableDictionary new];
_self->_allVisibleViewsDict = [NSMutableDictionary new];
_self->_cellClassDict = [NSMutableDictionary new];
_self->_cellNibDict = [NSMutableDictionary new];
_self->_supplementaryViewClassDict = [NSMutableDictionary new];
_self->_supplementaryViewNibDict = [NSMutableDictionary new];
// add class that saves additional ivars
objc_setAssociatedObject(_self, &kPSTColletionViewExt, [PSTCollectionViewExt new], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame collectionViewLayout:nil];
}
- (id)initWithFrame:(CGRect)frame collectionViewLayout:(PSTCollectionViewLayout *)layout {
if ((self = [super initWithFrame:frame])) {
// Set self as the UIScrollView's delegate
[super setDelegate:self];
PSTCollectionViewCommonSetup(self);
self.collectionViewLayout = layout;
_collectionViewData = [[PSTCollectionViewData alloc] initWithCollectionView:self layout:layout];
}
return self;
}
- (id)initWithCoder:(NSCoder *)inCoder {
if ((self = [super initWithCoder:inCoder])) {
// Set self as the UIScrollView's delegate
[super setDelegate:self];
PSTCollectionViewCommonSetup(self);
self.extVars.nibLayout = [inCoder decodeObjectForKey:@"UICollectionLayout"];
NSDictionary *cellExternalObjects = [inCoder decodeObjectForKey:@"UICollectionViewCellPrototypeNibExternalObjects"];
NSDictionary *cellNibs = [inCoder decodeObjectForKey:@"UICollectionViewCellNibDict"];
for (NSString *identifier in cellNibs.allKeys) {
_cellNibDict[identifier] = cellNibs[identifier];
}
self.extVars.nibCellsExternalObjects = cellExternalObjects;
NSDictionary *supplementaryViewExternalObjects = [inCoder decodeObjectForKey:@"UICollectionViewSupplementaryViewPrototypeNibExternalObjects"];
NSDictionary *supplementaryViewNibs = [inCoder decodeObjectForKey:@"UICollectionViewSupplementaryViewNibDict"];
for (NSString *identifier in supplementaryViewNibs.allKeys) {
_supplementaryViewNibDict[identifier] = supplementaryViewNibs[identifier];
}
self.extVars.supplementaryViewsExternalObjects = supplementaryViewExternalObjects;
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
PSTCollectionViewLayout *nibLayout = self.extVars.nibLayout;
if (nibLayout) {
self.collectionViewLayout = nibLayout;
self.extVars.nibLayout = nil;
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@ collection view layout: %@", [super description], self.collectionViewLayout];
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - UIView
- (void)layoutSubviews {
[super layoutSubviews];
// Adding alpha animation to make the relayouting smooth
if (_collectionViewFlags.fadeCellsForBoundsChange) {
CATransition *transition = [CATransition animation];
transition.duration = 0.25f * PSTSimulatorAnimationDragCoefficient();
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self.layer addAnimation:transition forKey:@"rotationAnimation"];
}
[_collectionViewData validateLayoutInRect:self.bounds];
// update cells
if (_collectionViewFlags.fadeCellsForBoundsChange) {
[CATransaction begin];
[CATransaction setDisableActions:YES];
}
if (!_collectionViewFlags.updatingLayout)
[self updateVisibleCellsNow:YES];
if (_collectionViewFlags.fadeCellsForBoundsChange) {
[CATransaction commit];
}
// do we need to update contentSize?
CGSize contentSize = [_collectionViewData collectionViewContentRect].size;
if (!CGSizeEqualToSize(self.contentSize, contentSize)) {
self.contentSize = contentSize;
// if contentSize is different, we need to re-evaluate layout, bounds (contentOffset) might changed
[_collectionViewData validateLayoutInRect:self.bounds];
[self updateVisibleCellsNow:YES];
}
if (_backgroundView) {
_backgroundView.frame = (CGRect){.origin=self.contentOffset, .size=self.bounds.size};
}
_collectionViewFlags.fadeCellsForBoundsChange = NO;
_collectionViewFlags.doneFirstLayout = YES;
}
- (void)setFrame:(CGRect)frame {
if (!CGRectEqualToRect(frame, self.frame)) {
[super setFrame:frame];
if ([self.collectionViewLayout shouldInvalidateLayoutForBoundsChange:self.bounds]) {
[self invalidateLayout];
_collectionViewFlags.fadeCellsForBoundsChange = YES;
}
}
}
- (void)setBounds:(CGRect)bounds {
if (!CGRectEqualToRect(bounds, self.bounds)) {
[super setBounds:bounds];
if ([self.collectionViewLayout shouldInvalidateLayoutForBoundsChange:bounds]) {
[self invalidateLayout];
_collectionViewFlags.fadeCellsForBoundsChange = YES;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[delegate scrollViewDidScroll:scrollView];
}
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidZoom:)]) {
[delegate scrollViewDidZoom:scrollView];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[delegate scrollViewWillBeginDragging:scrollView];
}
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
// Let collectionViewLayout decide where to stop.
*targetContentOffset = [[self collectionViewLayout] targetContentOffsetForProposedContentOffset:*targetContentOffset withScrollingVelocity:velocity];
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {
//if collectionViewDelegate implements this method, it may modify targetContentOffset as well
[delegate scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
[delegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
// if we are in the middle of a cell touch event, perform the "touchEnded" simulation
if (self.extVars.touchingIndexPath) {
[self cellTouchCancelled];
}
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)]) {
[delegate scrollViewWillBeginDecelerating:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[delegate scrollViewDidEndDecelerating:scrollView];
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)]) {
[delegate scrollViewDidEndScrollingAnimation:scrollView];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(viewForZoomingInScrollView:)]) {
return [delegate viewForZoomingInScrollView:scrollView];
}
return nil;
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewWillBeginZooming:withView:)]) {
[delegate scrollViewWillBeginZooming:scrollView withView:view];
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)]) {
[delegate scrollViewDidEndZooming:scrollView withView:view atScale:scale];
}
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewShouldScrollToTop:)]) {
return [delegate scrollViewShouldScrollToTop:scrollView];
}
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
id<PSTCollectionViewDelegate> delegate = self.extVars.collectionViewDelegate;
if ((id)delegate != self && [delegate respondsToSelector:@selector(scrollViewDidScrollToTop:)]) {
[delegate scrollViewDidScrollToTop:scrollView];
}
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier {
NSParameterAssert(cellClass);
NSParameterAssert(identifier);
_cellClassDict[identifier] = cellClass;
}
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier {
NSParameterAssert(viewClass);
NSParameterAssert(elementKind);
NSParameterAssert(identifier);
NSString *kindAndIdentifier = [NSString stringWithFormat:@"%@/%@", elementKind, identifier];
_supplementaryViewClassDict[kindAndIdentifier] = viewClass;
}
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier {
NSArray *topLevelObjects = [nib instantiateWithOwner:nil options:nil];
#pragma unused(topLevelObjects)
NSAssert(topLevelObjects.count == 1 && [topLevelObjects[0] isKindOfClass:PSTCollectionViewCell.class], @"must contain exactly 1 top level object which is a PSTCollectionViewCell");
_cellNibDict[identifier] = nib;
}
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier {
NSArray *topLevelObjects = [nib instantiateWithOwner:nil options:nil];
#pragma unused(topLevelObjects)
NSAssert(topLevelObjects.count == 1 && [topLevelObjects[0] isKindOfClass:PSTCollectionReusableView.class], @"must contain exactly 1 top level object which is a PSTCollectionReusableView");
NSString *kindAndIdentifier = [NSString stringWithFormat:@"%@/%@", kind, identifier];
_supplementaryViewNibDict[kindAndIdentifier] = nib;
}
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
// de-queue cell (if available)
NSMutableArray *reusableCells = _cellReuseQueues[identifier];
PSTCollectionViewCell *cell = [reusableCells lastObject];
PSTCollectionViewLayoutAttributes *attributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
if (cell) {
[reusableCells removeObjectAtIndex:reusableCells.count - 1];
}else {
if (_cellNibDict[identifier]) {
// Cell was registered via registerNib:forCellWithReuseIdentifier:
UINib *cellNib = _cellNibDict[identifier];
NSDictionary *externalObjects = self.extVars.nibCellsExternalObjects[identifier];
if (externalObjects) {
cell = [cellNib instantiateWithOwner:self options:@{UINibExternalObjects : externalObjects}][0];
}else {
cell = [cellNib instantiateWithOwner:self options:nil][0];
}
}else {
Class cellClass = _cellClassDict[identifier];
// compatibility layer
Class collectionViewCellClass = NSClassFromString(@"UICollectionViewCell");
if (collectionViewCellClass && [cellClass isEqual:collectionViewCellClass]) {
cellClass = PSTCollectionViewCell.class;
}
if (cellClass == nil) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Class not registered for identifier %@", identifier] userInfo:nil];
}
if (attributes) {
cell = [[cellClass alloc] initWithFrame:attributes.frame];
}else {
cell = [cellClass new];
}
}
cell.collectionView = self;
cell.reuseIdentifier = identifier;
}
[cell applyLayoutAttributes:attributes];
return cell;
}
- (id)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
NSString *kindAndIdentifier = [NSString stringWithFormat:@"%@/%@", elementKind, identifier];
NSMutableArray *reusableViews = _supplementaryViewReuseQueues[kindAndIdentifier];
PSTCollectionReusableView *view = [reusableViews lastObject];
if (view) {
[reusableViews removeObjectAtIndex:reusableViews.count - 1];
}else {
if (_supplementaryViewNibDict[kindAndIdentifier]) {
// supplementary view was registered via registerNib:forCellWithReuseIdentifier:
UINib *supplementaryViewNib = _supplementaryViewNibDict[kindAndIdentifier];
NSDictionary *externalObjects = self.extVars.supplementaryViewsExternalObjects[kindAndIdentifier];
if (externalObjects) {
view = [supplementaryViewNib instantiateWithOwner:self options:@{UINibExternalObjects : externalObjects}][0];
}else {
view = [supplementaryViewNib instantiateWithOwner:self options:nil][0];
}
}else {
Class viewClass = _supplementaryViewClassDict[kindAndIdentifier];
Class reusableViewClass = NSClassFromString(@"UICollectionReusableView");
if (reusableViewClass && [viewClass isEqual:reusableViewClass]) {
viewClass = PSTCollectionReusableView.class;
}
if (viewClass == nil) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Class not registered for kind/identifier %@", kindAndIdentifier] userInfo:nil];
}
if (self.collectionViewLayout) {
PSTCollectionViewLayoutAttributes *attributes = [self.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:elementKind atIndexPath:indexPath];
if (attributes) {
view = [[viewClass alloc] initWithFrame:attributes.frame];
}
}else {
view = [viewClass new];
}
}
view.collectionView = self;
view.reuseIdentifier = identifier;
}
return view;
}
- (id)dequeueReusableOrCreateDecorationViewOfKind:(NSString *)elementKind forIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *reusableViews = _decorationViewReuseQueues[elementKind];
PSTCollectionReusableView *view = [reusableViews lastObject];
PSTCollectionViewLayout *collectionViewLayout = self.collectionViewLayout;
PSTCollectionViewLayoutAttributes *attributes = [collectionViewLayout layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];
if (view) {
[reusableViews removeObjectAtIndex:reusableViews.count - 1];
}else {
NSDictionary *decorationViewNibDict = collectionViewLayout.decorationViewNibDict;
NSDictionary *decorationViewExternalObjects = collectionViewLayout.decorationViewExternalObjectsTables;
if (decorationViewNibDict[elementKind]) {
// supplementary view was registered via registerNib:forCellWithReuseIdentifier:
UINib *supplementaryViewNib = decorationViewNibDict[elementKind];
NSDictionary *externalObjects = decorationViewExternalObjects[elementKind];
if (externalObjects) {
view = [supplementaryViewNib instantiateWithOwner:self options:@{UINibExternalObjects : externalObjects}][0];
}else {
view = [supplementaryViewNib instantiateWithOwner:self options:nil][0];
}
}else {
NSDictionary *decorationViewClassDict = collectionViewLayout.decorationViewClassDict;
Class viewClass = decorationViewClassDict[elementKind];
Class reusableViewClass = NSClassFromString(@"UICollectionReusableView");
if (reusableViewClass && [viewClass isEqual:reusableViewClass]) {
viewClass = PSTCollectionReusableView.class;
}
if (viewClass == nil) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Class not registered for identifier %@", elementKind] userInfo:nil];
}
if (attributes) {
view = [[viewClass alloc] initWithFrame:attributes.frame];
}else {
view = [viewClass new];
}
}
view.collectionView = self;
view.reuseIdentifier = elementKind;
}
[view applyLayoutAttributes:attributes];
return view;
}
- (NSArray *)allCells {
return [[_allVisibleViewsDict allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject isKindOfClass:PSTCollectionViewCell.class];
}]];
}
- (NSArray *)visibleCells {
return [[_allVisibleViewsDict allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [evaluatedObject isKindOfClass:PSTCollectionViewCell.class] && CGRectIntersectsRect(self.bounds, [evaluatedObject frame]);
}]];
}
- (void)reloadData {
if (_reloadingSuspendedCount != 0) return;
[self invalidateLayout];
[_allVisibleViewsDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([obj isKindOfClass:UIView.class]) {
[obj removeFromSuperview];
}
}];
[_allVisibleViewsDict removeAllObjects];
for (NSIndexPath *indexPath in _indexPathsForSelectedItems) {
PSTCollectionViewCell *selectedCell = [self cellForItemAtIndexPath:indexPath];
selectedCell.selected = NO;
selectedCell.highlighted = NO;
}
[_indexPathsForSelectedItems removeAllObjects];
[_indexPathsForHighlightedItems removeAllObjects];
[self setNeedsLayout];
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Query Grid
- (NSInteger)numberOfSections {
return [_collectionViewData numberOfSections];
}
- (NSInteger)numberOfItemsInSection:(NSInteger)section {
return [_collectionViewData numberOfItemsInSection:section];
}
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
return [[self collectionViewLayout] layoutAttributesForItemAtIndexPath:indexPath];
}
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
return [[self collectionViewLayout] layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
}
- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point {
__block NSIndexPath *indexPath = nil;
[_allVisibleViewsDict enumerateKeysAndObjectsWithOptions:kNilOptions usingBlock:^(id key, id obj, BOOL *stop) {
PSTCollectionViewItemKey *itemKey = (PSTCollectionViewItemKey *)key;
if (itemKey.type == PSTCollectionViewItemTypeCell) {
PSTCollectionViewCell *cell = (PSTCollectionViewCell *)obj;
if (CGRectContainsPoint(cell.frame, point) && cell.userInteractionEnabled) {
indexPath = itemKey.indexPath;
*stop = YES;
}
}
}];
return indexPath;
}
- (NSIndexPath *)indexPathForCell:(PSTCollectionViewCell *)cell {
__block NSIndexPath *indexPath = nil;
[_allVisibleViewsDict enumerateKeysAndObjectsWithOptions:kNilOptions usingBlock:^(id key, id obj, BOOL *stop) {
PSTCollectionViewItemKey *itemKey = (PSTCollectionViewItemKey *)key;
if (itemKey.type == PSTCollectionViewItemTypeCell) {
PSTCollectionViewCell *currentCell = (PSTCollectionViewCell *)obj;
if (currentCell == cell) {
indexPath = itemKey.indexPath;
*stop = YES;
}
}
}];
return indexPath;
}
- (PSTCollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// NSInteger index = [_collectionViewData globalIndexForItemAtIndexPath:indexPath];
// TODO Apple uses some kind of globalIndex for this.
__block PSTCollectionViewCell *cell = nil;
[_allVisibleViewsDict enumerateKeysAndObjectsWithOptions:0 usingBlock:^(id key, id obj, BOOL *stop) {
PSTCollectionViewItemKey *itemKey = (PSTCollectionViewItemKey *)key;
if (itemKey.type == PSTCollectionViewItemTypeCell) {
if ([itemKey.indexPath isEqual:indexPath]) {
cell = obj;
*stop = YES;
}
}
}];
return cell;
}
- (NSArray *)indexPathsForVisibleItems {
NSArray *visibleCells = self.visibleCells;
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:visibleCells.count];
[visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PSTCollectionViewCell *cell = (PSTCollectionViewCell *)obj;
[indexPaths addObject:cell.layoutAttributes.indexPath];
}];
return indexPaths;
}
// returns nil or an array of selected index paths
- (NSArray *)indexPathsForSelectedItems {
return [_indexPathsForSelectedItems allObjects];
}
// Interacting with the collection view.
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(PSTCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
// Ensure grid is laid out; else we can't scroll.
[self layoutSubviews];
PSTCollectionViewLayoutAttributes *layoutAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
if (layoutAttributes) {
CGRect targetRect = [self makeRect:layoutAttributes.frame toScrollPosition:scrollPosition];
[self scrollRectToVisible:targetRect animated:animated];
}
}
- (CGRect)makeRect:(CGRect)targetRect toScrollPosition:(PSTCollectionViewScrollPosition)scrollPosition {
// split parameters
NSUInteger verticalPosition = scrollPosition&0x07; // 0000 0111
NSUInteger horizontalPosition = scrollPosition&0x38; // 0011 1000
if (verticalPosition != PSTCollectionViewScrollPositionNone
&& verticalPosition != PSTCollectionViewScrollPositionTop
&& verticalPosition != PSTCollectionViewScrollPositionCenteredVertically
&& verticalPosition != PSTCollectionViewScrollPositionBottom) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"PSTCollectionViewScrollPosition: attempt to use a scroll position with multiple vertical positioning styles" userInfo:nil];
}
if (horizontalPosition != PSTCollectionViewScrollPositionNone
&& horizontalPosition != PSTCollectionViewScrollPositionLeft
&& horizontalPosition != PSTCollectionViewScrollPositionCenteredHorizontally
&& horizontalPosition != PSTCollectionViewScrollPositionRight) {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"PSTCollectionViewScrollPosition: attempt to use a scroll position with multiple horizontal positioning styles" userInfo:nil];
}
CGRect frame = self.layer.bounds;
CGFloat calculateX;
CGFloat calculateY;
switch (verticalPosition) {
case PSTCollectionViewScrollPositionCenteredVertically:
calculateY = fmaxf(targetRect.origin.y - ((frame.size.height / 2) - (targetRect.size.height / 2)), -self.contentInset.top);
targetRect = CGRectMake(targetRect.origin.x, calculateY, targetRect.size.width, frame.size.height);
break;
case PSTCollectionViewScrollPositionTop:
targetRect = CGRectMake(targetRect.origin.x, targetRect.origin.y, targetRect.size.width, frame.size.height);
break;
case PSTCollectionViewScrollPositionBottom:
calculateY = fmaxf(targetRect.origin.y - (frame.size.height - targetRect.size.height), -self.contentInset.top);
targetRect = CGRectMake(targetRect.origin.x, calculateY, targetRect.size.width, frame.size.height);
break;
}
switch (horizontalPosition) {
case PSTCollectionViewScrollPositionCenteredHorizontally:
calculateX = targetRect.origin.x - ((frame.size.width / 2) - (targetRect.size.width / 2));
targetRect = CGRectMake(calculateX, targetRect.origin.y, frame.size.width, targetRect.size.height);
break;
case PSTCollectionViewScrollPositionLeft:
targetRect = CGRectMake(targetRect.origin.x, targetRect.origin.y, frame.size.width, targetRect.size.height);
break;
case PSTCollectionViewScrollPositionRight:
calculateX = targetRect.origin.x - (frame.size.width - targetRect.size.width);
targetRect = CGRectMake(calculateX, targetRect.origin.y, frame.size.width, targetRect.size.height);
break;
}
return targetRect;
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Touch Handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
// reset touching state vars
self.extVars.touchingIndexPath = nil;
self.extVars.currentIndexPath = nil;
CGPoint touchPoint = [[touches anyObject] locationInView:self];
NSIndexPath *indexPath = [self indexPathForItemAtPoint:touchPoint];
if (indexPath && self.allowsSelection) {
if (![self highlightItemAtIndexPath:indexPath animated:YES scrollPosition:PSTCollectionViewScrollPositionNone notifyDelegate:YES])
return;
self.extVars.touchingIndexPath = indexPath;
self.extVars.currentIndexPath = indexPath;
if (!self.allowsMultipleSelection) {
// temporally unhighlight background on touchesBegan (keeps selected by _indexPathsForSelectedItems)
// single-select only mode only though
NSIndexPath *tempDeselectIndexPath = _indexPathsForSelectedItems.anyObject;
if (tempDeselectIndexPath && ![tempDeselectIndexPath isEqual:self.extVars.touchingIndexPath]) {
// iOS6 UICollectionView deselects cell without notification
PSTCollectionViewCell *selectedCell = [self cellForItemAtIndexPath:tempDeselectIndexPath];
selectedCell.selected = NO;
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
// allows moving between highlight and unhighlight state only if setHighlighted is not overwritten
if (self.extVars.touchingIndexPath) {
CGPoint touchPoint = [[touches anyObject] locationInView:self];
NSIndexPath *indexPath = [self indexPathForItemAtPoint:touchPoint];
// moving out of bounds
if ([self.extVars.currentIndexPath isEqual:self.extVars.touchingIndexPath] &&
![indexPath isEqual:self.extVars.touchingIndexPath] &&
[self unhighlightItemAtIndexPath:self.extVars.touchingIndexPath animated:YES notifyDelegate:YES shouldCheckHighlight:YES]) {
self.extVars.currentIndexPath = indexPath;
// moving back into the original touching cell
}else if (![self.extVars.currentIndexPath isEqual:self.extVars.touchingIndexPath] &&
[indexPath isEqual:self.extVars.touchingIndexPath]) {
[self highlightItemAtIndexPath:self.extVars.touchingIndexPath animated:YES scrollPosition:PSTCollectionViewScrollPositionNone notifyDelegate:YES];
self.extVars.currentIndexPath = self.extVars.touchingIndexPath;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
if (self.extVars.touchingIndexPath) {
// first unhighlight the touch operation
[self unhighlightItemAtIndexPath:self.extVars.touchingIndexPath animated:YES notifyDelegate:YES];
CGPoint touchPoint = [[touches anyObject] locationInView:self];
NSIndexPath *indexPath = [self indexPathForItemAtPoint:touchPoint];
if ([indexPath isEqual:self.extVars.touchingIndexPath]) {
[self userSelectedItemAtIndexPath:indexPath];
}
else if (!self.allowsMultipleSelection) {
NSIndexPath *tempDeselectIndexPath = _indexPathsForSelectedItems.anyObject;
if (tempDeselectIndexPath && ![tempDeselectIndexPath isEqual:self.extVars.touchingIndexPath]) {
[self cellTouchCancelled];
}
}
// for pedantic reasons only - always set to nil on touchesBegan
self.extVars.touchingIndexPath = nil;
self.extVars.currentIndexPath = nil;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
// do not mark touchingIndexPath as nil because whoever cancelled this touch will need to signal a touch up event later
if (self.extVars.touchingIndexPath) {
// first unhighlight the touch operation
[self unhighlightItemAtIndexPath:self.extVars.touchingIndexPath animated:YES notifyDelegate:YES];
}
}
- (void)cellTouchCancelled {
// turn on ALL the *should be selected* cells (iOS6 UICollectionView does no state keeping or other fancy optimizations)
// there should be no notifications as this is a silent "turn everything back on"
for (NSIndexPath *tempDeselectedIndexPath in [_indexPathsForSelectedItems copy]) {
PSTCollectionViewCell *selectedCell = [self cellForItemAtIndexPath:tempDeselectedIndexPath];
selectedCell.selected = YES;
}
}
- (void)userSelectedItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.allowsMultipleSelection && [_indexPathsForSelectedItems containsObject:indexPath]) {
[self deselectItemAtIndexPath:indexPath animated:YES notifyDelegate:YES];
}
else if (self.allowsSelection) {
[self selectItemAtIndexPath:indexPath animated:YES scrollPosition:PSTCollectionViewScrollPositionNone notifyDelegate:YES];
}
}
// select item, notify delegate (internal)
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(PSTCollectionViewScrollPosition)scrollPosition notifyDelegate:(BOOL)notifyDelegate {
if (self.allowsMultipleSelection && [_indexPathsForSelectedItems containsObject:indexPath]) {
BOOL shouldDeselect = YES;
if (notifyDelegate && _collectionViewFlags.delegateShouldDeselectItemAtIndexPath) {
shouldDeselect = [self.delegate collectionView:self shouldDeselectItemAtIndexPath:indexPath];
}
if (shouldDeselect) {
[self deselectItemAtIndexPath:indexPath animated:animated notifyDelegate:notifyDelegate];
}
}
else {
// either single selection, or wasn't already selected in multiple selection mode
BOOL shouldSelect = YES;
if (notifyDelegate && _collectionViewFlags.delegateShouldSelectItemAtIndexPath) {
shouldSelect = [self.delegate collectionView:self shouldSelectItemAtIndexPath:indexPath];
}
if (!self.allowsMultipleSelection) {
// now unselect the previously selected cell for single selection
NSIndexPath *tempDeselectIndexPath = _indexPathsForSelectedItems.anyObject;
if (tempDeselectIndexPath && ![tempDeselectIndexPath isEqual:indexPath]) {
[self deselectItemAtIndexPath:tempDeselectIndexPath animated:YES notifyDelegate:YES];
}
}
if (shouldSelect) {
PSTCollectionViewCell *selectedCell = [self cellForItemAtIndexPath:indexPath];
selectedCell.selected = YES;
[_indexPathsForSelectedItems addObject:indexPath];
[selectedCell performSelectionSegue];
if (scrollPosition != PSTCollectionViewScrollPositionNone) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
if (notifyDelegate && _collectionViewFlags.delegateDidSelectItemAtIndexPath) {
[self.delegate collectionView:self didSelectItemAtIndexPath:indexPath];
}
}
}
}
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(PSTCollectionViewScrollPosition)scrollPosition {
[self selectItemAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition notifyDelegate:NO];
}
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated {
[self deselectItemAtIndexPath:indexPath animated:animated notifyDelegate:NO];
}
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated notifyDelegate:(BOOL)notifyDelegate {
BOOL shouldDeselect = YES;
// deselect only relevant during multi mode
if (self.allowsMultipleSelection && notifyDelegate && _collectionViewFlags.delegateShouldDeselectItemAtIndexPath) {
shouldDeselect = [self.delegate collectionView:self shouldDeselectItemAtIndexPath:indexPath];
}
if (shouldDeselect && [_indexPathsForSelectedItems containsObject:indexPath]) {
PSTCollectionViewCell *selectedCell = [self cellForItemAtIndexPath:indexPath];
if (selectedCell) {
if (selectedCell.selected) {
selectedCell.selected = NO;
}
}
[_indexPathsForSelectedItems removeObject:indexPath];
if (notifyDelegate && _collectionViewFlags.delegateDidDeselectItemAtIndexPath) {
[self.delegate collectionView:self didDeselectItemAtIndexPath:indexPath];
}
}
}
- (BOOL)highlightItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(PSTCollectionViewScrollPosition)scrollPosition notifyDelegate:(BOOL)notifyDelegate {
BOOL shouldHighlight = YES;
if (notifyDelegate && _collectionViewFlags.delegateShouldHighlightItemAtIndexPath) {
shouldHighlight = [self.delegate collectionView:self shouldHighlightItemAtIndexPath:indexPath];
}
if (shouldHighlight) {
PSTCollectionViewCell *highlightedCell = [self cellForItemAtIndexPath:indexPath];
highlightedCell.highlighted = YES;
[_indexPathsForHighlightedItems addObject:indexPath];
if (scrollPosition != PSTCollectionViewScrollPositionNone) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}
if (notifyDelegate && _collectionViewFlags.delegateDidHighlightItemAtIndexPath) {
[self.delegate collectionView:self didHighlightItemAtIndexPath:indexPath];
}
}
return shouldHighlight;
}
- (BOOL)unhighlightItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated notifyDelegate:(BOOL)notifyDelegate {
return [self unhighlightItemAtIndexPath:indexPath animated:animated notifyDelegate:notifyDelegate shouldCheckHighlight:NO];
}
- (BOOL)unhighlightItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated notifyDelegate:(BOOL)notifyDelegate shouldCheckHighlight:(BOOL)check {
if ([_indexPathsForHighlightedItems containsObject:indexPath]) {
PSTCollectionViewCell *highlightedCell = [self cellForItemAtIndexPath:indexPath];
// iOS6 does not notify any delegate if the cell was never highlighted (setHighlighted overwritten) during touchMoved
if (check && !highlightedCell.highlighted) {
return NO;
}
// if multiple selection or not unhighlighting a selected item we don't perform any op
if (highlightedCell.highlighted && [_indexPathsForSelectedItems containsObject:indexPath]) {
highlightedCell.highlighted = YES;
}else {
highlightedCell.highlighted = NO;
}
[_indexPathsForHighlightedItems removeObject:indexPath];
if (notifyDelegate && _collectionViewFlags.delegateDidUnhighlightItemAtIndexPath) {
[self.delegate collectionView:self didUnhighlightItemAtIndexPath:indexPath];
}
return YES;
}
return NO;
}
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Update Grid
- (void)insertSections:(NSIndexSet *)sections {
[self updateSections:sections updateAction:PSTCollectionUpdateActionInsert];
}
- (void)deleteSections:(NSIndexSet *)sections {
// First delete all items
NSMutableArray *paths = [NSMutableArray new];
[sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
for (int i = 0; i < [self numberOfItemsInSection:idx]; ++i) {
[paths addObject:[NSIndexPath indexPathForItem:i inSection:idx]];
}
}];
[self deleteItemsAtIndexPaths:paths];
// Then delete the section.
[self updateSections:sections updateAction:PSTCollectionUpdateActionDelete];
}
- (void)reloadSections:(NSIndexSet *)sections {
[self updateSections:sections updateAction:PSTCollectionUpdateActionReload];