Skip to content

Commit

Permalink
Showing 5 changed files with 115 additions and 19 deletions.
22 changes: 18 additions & 4 deletions DTCollectionViewManager/DTCollectionViewController.h
Original file line number Diff line number Diff line change
@@ -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
///---------------------------------------
16 changes: 16 additions & 0 deletions DTCollectionViewManager/DTCollectionViewController.m
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions DTCollectionViewManager/DTCollectionViewFactory.h
Original file line number Diff line number Diff line change
@@ -51,6 +51,11 @@
forKind:(NSString *)kind
forModelClass:(Class)modelClass;

- (void)registerNibNamed:(NSString *)nibName
forSupplementaryClass:(Class)supplementaryClass
forKind:(NSString *)kind
forModelClass:(Class)modelClass;

- (UICollectionViewCell <DTModelTransfer> *)cellForItem:(id)modelItem
atIndexPath:(NSIndexPath *)indexPath;

40 changes: 25 additions & 15 deletions DTCollectionViewManager/DTCollectionViewFactory.m
Original file line number Diff line number Diff line change
@@ -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 <DTModelTransfer> *)cellForItem:(id)modelItem
atIndexPath:(NSIndexPath *)indexPath
{
51 changes: 51 additions & 0 deletions Example/CedarUnitTests/Specs/Mapping.mm
Original file line number Diff line number Diff line change
@@ -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 <UICollectionViewDataSource> 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 <UICollectionViewDataSource> 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]

0 comments on commit 6759e4c

Please sign in to comment.