diff --git a/DTCollectionViewManager/DTCollectionViewController.h b/DTCollectionViewManager/DTCollectionViewController.h index af3b786..b4954f1 100644 --- a/DTCollectionViewManager/DTCollectionViewController.h +++ b/DTCollectionViewManager/DTCollectionViewController.h @@ -132,14 +132,11 @@ @param cellClass Class of the cell you want to be created for model with modelClass. @param modelClass Class of the model you want to be mapped to cellClass. - - @discussion This is the designated mapping method. Best place to call it - in viewDidLoad method. - */ -(void)registerNibNamed:(NSString *)nibName forCellClass:(Class)cellClass forModelClass:(Class)modelClass; /** - This method registers `supplementaryClass` for UICollectionView supplementary `kind`. `supplementaryClass` should be a UICollectionReusableView subclass, conforming to `DTModelTransfer` protocol. + This method registers `supplementaryClass` for UICollectionView supplementary `kind`. `supplementaryClass` should be a UICollectionReusableView subclass, conforming to `DTModelTransfer` protocol. xib file for supplementary class with `supplementaryClass` name is automatically detected if it exists. @param supplementaryClass UICollectionReusableView subclass to be mapped for `modelClass`. @@ -152,6 +149,23 @@ forKind:(NSString *)kind forModelClass:(Class)modelClass; +/** + This method registers `supplementaryClass` for UICollectionView supplementary `kind`. `supplementaryClass` should be a UICollectionReusableView subclass, conforming to `DTModelTransfer` protocol. + + @param nibName name of the nib file to be used + + @param supplementaryClass UICollectionReusableView subclass to be mapped for `modelClass`. + + @param kind UICollectionView supplementary view kind. + + @param modelClass modelClass to be mapped to `supplementaryClass` + + */ +-(void)registerNibNamed:(NSString *)nibName + forSupplementaryClass:(Class)supplementaryClass + forKind:(NSString *)kind + forModelClass:(Class)modelClass; + ///--------------------------------------- /// @name Search ///--------------------------------------- diff --git a/DTCollectionViewManager/DTCollectionViewController.m b/DTCollectionViewManager/DTCollectionViewController.m index 71c9f38..ac47a38 100644 --- a/DTCollectionViewManager/DTCollectionViewController.m +++ b/DTCollectionViewManager/DTCollectionViewController.m @@ -132,6 +132,22 @@ -(void)registerSupplementaryClass:(Class)supplementaryClass forKind:(NSString *) forModelClass:modelClass]; } +-(void)registerNibNamed:(NSString *)nibName forSupplementaryClass:(Class)supplementaryClass + forKind:(NSString *)kind + forModelClass:(Class)modelClass +{ + NSParameterAssert(nibName.length > 0); + NSParameterAssert([supplementaryClass isSubclassOfClass:[UICollectionReusableView class]]); + NSParameterAssert([supplementaryClass conformsToProtocol:@protocol(DTModelTransfer)]); + NSParameterAssert(kind); + NSParameterAssert(modelClass); + + [self.factory registerNibNamed:nibName + forSupplementaryClass:supplementaryClass + forKind:kind + forModelClass:modelClass]; +} + #pragma mark - search -(BOOL)isSearching diff --git a/DTCollectionViewManager/DTCollectionViewFactory.h b/DTCollectionViewManager/DTCollectionViewFactory.h index 4c7ddab..68f94d6 100644 --- a/DTCollectionViewManager/DTCollectionViewFactory.h +++ b/DTCollectionViewManager/DTCollectionViewFactory.h @@ -51,6 +51,11 @@ forKind:(NSString *)kind forModelClass:(Class)modelClass; +- (void)registerNibNamed:(NSString *)nibName + forSupplementaryClass:(Class)supplementaryClass + forKind:(NSString *)kind + forModelClass:(Class)modelClass; + - (UICollectionViewCell *)cellForItem:(id)modelItem atIndexPath:(NSIndexPath *)indexPath; diff --git a/DTCollectionViewManager/DTCollectionViewFactory.m b/DTCollectionViewManager/DTCollectionViewFactory.m index a5d9f5a..b91eaa4 100644 --- a/DTCollectionViewManager/DTCollectionViewFactory.m +++ b/DTCollectionViewManager/DTCollectionViewFactory.m @@ -81,21 +81,12 @@ - (void)registerCellClass:(Class)cellClass forModelClass:(Class)modelClass - (void)registerNibNamed:(NSString *)nibName forCellClass:(Class)cellClass forModelClass:(Class)modelClass { - if ([self nibExistsWithNibName:nibName]) - { - [[self.delegate collectionView] registerNib:[UINib nibWithNibName:nibName bundle:nil] - forCellWithReuseIdentifier:[self reuseIdentifierFromClass:cellClass]]; - self.cellMappings[[self classStringForClass:modelClass]] = NSStringFromClass(cellClass); - } - else - { - NSString * reason = [NSString stringWithFormat:@"nib named %@ not found", nibName]; - NSException * exc = - [NSException exceptionWithName:@"DTCollectionViewManager API exception" - reason:reason - userInfo:nil]; - [exc raise]; - } + BOOL nibExists = [self nibExistsWithNibName:nibName]; + NSParameterAssert(nibExists); + + [[self.delegate collectionView] registerNib:[UINib nibWithNibName:nibName bundle:nil] + forCellWithReuseIdentifier:[self reuseIdentifierFromClass:cellClass]]; + self.cellMappings[[self classStringForClass:modelClass]] = NSStringFromClass(cellClass); } - (void)registerSupplementaryClass:(Class)supplementaryClass @@ -116,6 +107,25 @@ - (void)registerSupplementaryClass:(Class)supplementaryClass forModelClass:modelClass]; } +- (void)registerNibNamed:(NSString *)nibName + forSupplementaryClass:(Class)supplementaryClass + forKind:(NSString *)kind + forModelClass:(Class)modelClass +{ + BOOL nibExists = [self nibExistsWithNibName:nibName]; + + NSParameterAssert(nibExists); + + [[self.delegate collectionView] registerNib:[UINib nibWithNibName:nibName + bundle:nil] + forSupplementaryViewOfKind:kind + withReuseIdentifier:[self reuseIdentifierFromClass:supplementaryClass]]; + + [self setSupplementaryClass:supplementaryClass + forKind:kind + forModelClass:modelClass]; +} + - (UICollectionViewCell *)cellForItem:(id)modelItem atIndexPath:(NSIndexPath *)indexPath { diff --git a/Example/CedarUnitTests/Specs/Mapping.mm b/Example/CedarUnitTests/Specs/Mapping.mm index 1892d64..f51aa38 100644 --- a/Example/CedarUnitTests/Specs/Mapping.mm +++ b/Example/CedarUnitTests/Specs/Mapping.mm @@ -145,6 +145,57 @@ header.awakenFromNib should BeTruthy(); }); + it(@"should be able to register supplementary header nib class with custom nib", ^{ + [collection registerNibNamed:@"SupplementaryViewWithNib" + forSupplementaryClass:[SupplementaryViewWithNib class] + forKind:UICollectionElementKindSectionHeader + forModelClass:[Model class]]; + + [collection.memoryStorage updateWithoutAnimations:^{ + [collection.memoryStorage setSupplementaries:@[[[Model new] autorelease]] + forKind:UICollectionElementKindSectionHeader]; + [collection.memoryStorage addItem:[[Model new] autorelease]]; + + }]; + + [collection.collectionView reloadData]; + [collection.collectionView performBatchUpdates:nil completion:nil]; + + id datasource = collection.collectionView.dataSource; + UIView * view = [datasource collectionView:collection.collectionView + viewForSupplementaryElementOfKind:UICollectionElementKindSectionHeader + atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; + SupplementaryView * header = (SupplementaryView *) view; + [header class] should equal([SupplementaryViewWithNib class]); + header.inittedWithFrame should_not BeTruthy(); + header.awakenFromNib should BeTruthy(); + }); + + it(@"should be able to register supplementary footer nib class with custom nib", ^{ + [collection registerNibNamed:@"SupplementaryViewWithNib" + forSupplementaryClass:[SupplementaryViewWithNib class] + forKind:UICollectionElementKindSectionFooter + forModelClass:[Model class]]; + + [collection.memoryStorage updateWithoutAnimations:^{ + [collection.memoryStorage setSupplementaries:@[[[Model new] autorelease]] + forKind:UICollectionElementKindSectionFooter]; + [collection.memoryStorage addItem:[[Model new] autorelease]]; + }]; + + [collection.collectionView reloadData]; + [collection.collectionView performBatchUpdates:nil completion:nil]; + + id datasource = collection.collectionView.dataSource; + UIView * view = [datasource collectionView:collection.collectionView + viewForSupplementaryElementOfKind:UICollectionElementKindSectionFooter + atIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; + SupplementaryView * header = (SupplementaryView *) view; + [header class] should equal([SupplementaryViewWithNib class]); + header.inittedWithFrame should_not BeTruthy(); + header.awakenFromNib should BeTruthy(); + }); + it(@"should not be able to register wrong class", ^{ ^{ [collection registerSupplementaryClass:[NSString class]