PSTCollectionViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // PSTCollectionViewController.m
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTCollectionViewController.h"
  8. #import "PSTCollectionView.h"
  9. @interface PSTCollectionViewController () {
  10. PSTCollectionViewLayout *_layout;
  11. PSTCollectionView *_collectionView;
  12. struct {
  13. unsigned int clearsSelectionOnViewWillAppear : 1;
  14. unsigned int appearsFirstTime : 1; // PST extension!
  15. }_collectionViewControllerFlags;
  16. char filler[100]; // [HACK] Our class needs to be larger than Apple's class for the superclass change to work.
  17. }
  18. @property (nonatomic, strong) PSTCollectionViewLayout *layout;
  19. @end
  20. @implementation PSTCollectionViewController
  21. ///////////////////////////////////////////////////////////////////////////////////////////
  22. #pragma mark - NSObject
  23. - (id)initWithCoder:(NSCoder *)coder {
  24. self = [super initWithCoder:coder];
  25. if (self) {
  26. self.layout = [PSTCollectionViewFlowLayout new];
  27. self.clearsSelectionOnViewWillAppear = YES;
  28. _collectionViewControllerFlags.appearsFirstTime = YES;
  29. }
  30. return self;
  31. }
  32. - (id)initWithCollectionViewLayout:(PSTCollectionViewLayout *)layout {
  33. if ((self = [super init])) {
  34. self.layout = layout;
  35. self.clearsSelectionOnViewWillAppear = YES;
  36. _collectionViewControllerFlags.appearsFirstTime = YES;
  37. }
  38. return self;
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////////////////
  41. #pragma mark - UIViewController
  42. - (void)loadView {
  43. [super loadView];
  44. // if this is restored from IB, we don't have plain main view.
  45. if ([self.view isKindOfClass:PSTCollectionView.class]) {
  46. _collectionView = (PSTCollectionView *)self.view;
  47. self.view = [[UIView alloc] initWithFrame:self.view.bounds];
  48. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  49. }
  50. if (_collectionView.delegate == nil) _collectionView.delegate = self;
  51. if (_collectionView.dataSource == nil) _collectionView.dataSource = self;
  52. // only create the collection view if it is not already created (by IB)
  53. if (!_collectionView) {
  54. self.collectionView = [[PSTCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.layout];
  55. self.collectionView.delegate = self;
  56. self.collectionView.dataSource = self;
  57. }
  58. }
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. // This seems like a hack, but is needed for real compatibility
  62. // There can be implementations of loadView that don't call super and don't set the view, yet it works in UICollectionViewController.
  63. if (!self.isViewLoaded) {
  64. self.view = [[UIView alloc] initWithFrame:CGRectZero];
  65. }
  66. // Attach the view
  67. if (self.view != self.collectionView) {
  68. [self.view addSubview:self.collectionView];
  69. self.collectionView.frame = self.view.bounds;
  70. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  71. }
  72. }
  73. - (void)viewWillAppear:(BOOL)animated {
  74. [super viewWillAppear:animated];
  75. if (_collectionViewControllerFlags.appearsFirstTime) {
  76. [_collectionView reloadData];
  77. _collectionViewControllerFlags.appearsFirstTime = NO;
  78. }
  79. if (_collectionViewControllerFlags.clearsSelectionOnViewWillAppear) {
  80. for (NSIndexPath *aIndexPath in [[_collectionView indexPathsForSelectedItems] copy]) {
  81. [_collectionView deselectItemAtIndexPath:aIndexPath animated:animated];
  82. }
  83. }
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////////////////
  86. #pragma mark - Lazy load the collection view
  87. - (PSTCollectionView *)collectionView {
  88. if (!_collectionView) {
  89. _collectionView = [[PSTCollectionView alloc] initWithFrame:UIScreen.mainScreen.bounds collectionViewLayout:self.layout];
  90. _collectionView.delegate = self;
  91. _collectionView.dataSource = self;
  92. // If the collection view isn't the main view, add it.
  93. if (self.isViewLoaded && self.view != self.collectionView) {
  94. [self.view addSubview:self.collectionView];
  95. self.collectionView.frame = self.view.bounds;
  96. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  97. }
  98. }
  99. return _collectionView;
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////////////////
  102. #pragma mark - Properties
  103. - (void)setClearsSelectionOnViewWillAppear:(BOOL)clearsSelectionOnViewWillAppear {
  104. _collectionViewControllerFlags.clearsSelectionOnViewWillAppear = (unsigned int)clearsSelectionOnViewWillAppear;
  105. }
  106. - (BOOL)clearsSelectionOnViewWillAppear {
  107. return _collectionViewControllerFlags.clearsSelectionOnViewWillAppear;
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////////////////
  110. #pragma mark - PSTCollectionViewDataSource
  111. - (NSInteger)collectionView:(PSTCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  112. return 0;
  113. }
  114. - (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  115. [self doesNotRecognizeSelector:_cmd];
  116. return nil;
  117. }
  118. @end