PSTCollectionView.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // PSTCollectionView.h
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTCollectionViewLayout.h"
  8. #import "PSTCollectionViewFlowLayout.h"
  9. #import "PSTCollectionViewCell.h"
  10. #import "PSTCollectionViewController.h"
  11. #import "PSTCollectionViewUpdateItem.h"
  12. @class PSTCollectionViewController;
  13. typedef NS_OPTIONS(NSUInteger, PSTCollectionViewScrollPosition) {
  14. PSTCollectionViewScrollPositionNone = 0,
  15. // The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
  16. // Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
  17. PSTCollectionViewScrollPositionTop = 1 << 0,
  18. PSTCollectionViewScrollPositionCenteredVertically = 1 << 1,
  19. PSTCollectionViewScrollPositionBottom = 1 << 2,
  20. // Likewise, the horizontal positions are mutually exclusive to each other.
  21. PSTCollectionViewScrollPositionLeft = 1 << 3,
  22. PSTCollectionViewScrollPositionCenteredHorizontally = 1 << 4,
  23. PSTCollectionViewScrollPositionRight = 1 << 5
  24. };
  25. typedef NS_ENUM(NSUInteger, PSTCollectionElementCategory) {
  26. PSTCollectionElementCategoryCell,
  27. PSTCollectionElementCategorySupplementaryView,
  28. PSTCollectionElementCategoryDecorationView
  29. };
  30. // Define the `PSTCollectionViewDisableForwardToUICollectionViewSentinel` to disable the automatic forwarding to UICollectionView on iOS 6+. (Copy below line into your AppDelegate.m)
  31. //@interface PSTCollectionViewDisableForwardToUICollectionViewSentinel : NSObject @end @implementation PSTCollectionViewDisableForwardToUICollectionViewSentinel @end
  32. // API-compatible replacement for UICollectionView.
  33. // Works on iOS 4.3 upwards (including iOS 6).
  34. @interface PSTCollectionView : UIScrollView
  35. - (id)initWithFrame:(CGRect)frame collectionViewLayout:(PSTCollectionViewLayout *)layout; // the designated initializer
  36. @property (nonatomic, strong) PSTCollectionViewLayout *collectionViewLayout;
  37. @property (nonatomic, assign) IBOutlet id<PSTCollectionViewDelegate> delegate;
  38. @property (nonatomic, assign) IBOutlet id<PSTCollectionViewDataSource> dataSource;
  39. @property (nonatomic, strong) UIView *backgroundView; // will be automatically resized to track the size of the collection view and placed behind all cells and supplementary views.
  40. // For each reuse identifier that the collection view will use, register either a class or a nib from which to instantiate a cell.
  41. // If a nib is registered, it must contain exactly 1 top level object which is a PSTCollectionViewCell.
  42. // If a class is registered, it will be instantiated via alloc/initWithFrame:
  43. - (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
  44. - (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
  45. - (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
  46. // TODO: implement!
  47. - (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
  48. - (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
  49. - (id)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
  50. // These properties control whether items can be selected, and if so, whether multiple items can be simultaneously selected.
  51. @property (nonatomic) BOOL allowsSelection; // default is YES
  52. @property (nonatomic) BOOL allowsMultipleSelection; // default is NO
  53. - (NSArray *)indexPathsForSelectedItems; // returns nil or an array of selected index paths
  54. - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(PSTCollectionViewScrollPosition)scrollPosition;
  55. - (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
  56. - (void)reloadData; // discard the dataSource and delegate data and requery as necessary
  57. - (void)setCollectionViewLayout:(PSTCollectionViewLayout *)layout animated:(BOOL)animated; // transition from one layout to another
  58. // Information about the current state of the collection view.
  59. - (NSInteger)numberOfSections;
  60. - (NSInteger)numberOfItemsInSection:(NSInteger)section;
  61. - (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
  62. - (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
  63. - (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;
  64. - (NSIndexPath *)indexPathForCell:(PSTCollectionViewCell *)cell;
  65. - (PSTCollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
  66. - (NSArray *)visibleCells;
  67. - (NSArray *)indexPathsForVisibleItems;
  68. // Interacting with the collection view.
  69. - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(PSTCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
  70. // These methods allow dynamic modification of the current set of items in the collection view
  71. - (void)insertSections:(NSIndexSet *)sections;
  72. - (void)deleteSections:(NSIndexSet *)sections;
  73. - (void)reloadSections:(NSIndexSet *)sections;
  74. - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
  75. - (void)insertItemsAtIndexPaths:(NSArray *)indexPaths;
  76. - (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths;
  77. - (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths;
  78. - (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
  79. - (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion; // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.
  80. @end
  81. // To dynamically switch between PSTCollectionView and UICollectionView, use the PSUICollectionView* classes.
  82. #define PSUICollectionView PSUICollectionView_
  83. #define PSUICollectionViewCell PSUICollectionViewCell_
  84. #define PSUICollectionReusableView PSUICollectionReusableView_
  85. #define PSUICollectionViewDelegate PSTCollectionViewDelegate
  86. #define PSUICollectionViewDataSource PSTCollectionViewDataSource
  87. #define PSUICollectionViewLayout PSUICollectionViewLayout_
  88. #define PSUICollectionViewFlowLayout PSUICollectionViewFlowLayout_
  89. #define PSUICollectionViewDelegateFlowLayout PSTCollectionViewDelegateFlowLayout
  90. #define PSUICollectionViewLayoutAttributes PSUICollectionViewLayoutAttributes_
  91. #define PSUICollectionViewController PSUICollectionViewController_
  92. @interface PSUICollectionView_ : PSTCollectionView @end
  93. @interface PSUICollectionViewCell_ : PSTCollectionViewCell @end
  94. @interface PSUICollectionReusableView_ : PSTCollectionReusableView @end
  95. @interface PSUICollectionViewLayout_ : PSTCollectionViewLayout @end
  96. @interface PSUICollectionViewFlowLayout_ : PSTCollectionViewFlowLayout @end
  97. @protocol PSUICollectionViewDelegateFlowLayout_ <PSTCollectionViewDelegateFlowLayout> @end
  98. @interface PSUICollectionViewLayoutAttributes_ : PSTCollectionViewLayoutAttributes @end
  99. @interface PSUICollectionViewController_ : PSTCollectionViewController <PSUICollectionViewDelegate, PSUICollectionViewDataSource> @end