Custom UICollectionReusableView From XIBs
Here is how to extend UICollectionReusableView in an intelligent way which allows for the easy use of Xibs in collection view headers and footers.
- ios
Originally published at rexstjohn.com on January 7, 2014. Recovered from the Internet Archive.
Here is how to extend UICollectionReusableView in an intelligent way which allows for the easy use of Xibs in collection view headers and footers. I am using a variation of the “Smart Table View Cell” pattern which relegates responsibility for registering and dequeuing custom Xib-based table cell classes to the cell class itself and not the parent view controller.
Header File:
#import <UIKit/UIKit.h>
@interface UXRSmartCollectionReuseableView : UICollectionReusableView
+(NSString*)cellIdentifier;
+(id)collectionReusableViewForCollectionView:(UICollectionView*)collectionView
fromNib:(UINib*)nib
forIndexPath:(NSIndexPath*)indexPath
withKind:(NSString*)kind;
+(id)collectionReusableViewForCollectionView:(UICollectionView*)collectionView
forIndexPath:(NSIndexPath*)indexPath withKind:(NSString*)kind;
+(UINib*)nib;
@end
Implementation file:
#import "UXRSmartCollectionReuseableView.h"
@implementation UXRSmartCollectionReuseableView
+(id)collectionReusableViewForCollectionView:(UICollectionView*)collectionView
fromNib:(UINib*)nib
forIndexPath:(NSIndexPath*)indexPath
withKind:(NSString*)kind{
NSString *cellIdentifier = [self cellIdentifier];
[collectionView registerClass:[self class] forSupplementaryViewOfKind:kind withReuseIdentifier:cellIdentifier];
[collectionView registerNib:nib forSupplementaryViewOfKind:kind withReuseIdentifier:cellIdentifier];
UXRSmartCollectionReuseableView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
}
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
[super applyLayoutAttributes:layoutAttributes];
//
}
+(id)collectionReusableViewForCollectionView:(UICollectionView*)collectionView
forIndexPath:(NSIndexPath*)indexPath withKind:(NSString*)kind{
return [[self class] collectionReusableViewForCollectionView:collectionView
fromNib:[self nib]
forIndexPath:indexPath
withKind:kind];
}
+ (NSString *)nibName {
return [self cellIdentifier];
}
+ (NSString *)cellIdentifier {
[NSException raise:NSInternalInconsistencyException format:@"WARNING: YOU MUST OVERRIDE THIS GETTER IN YOUR CUSTOM CELL .M FILE"];
static NSString* _cellIdentifier = nil;
_cellIdentifier = NSStringFromClass([self class]);
return _cellIdentifier;
}
+(UINib*)nib{
NSBundle * classBundle = [NSBundle bundleForClass:[self class]];
UINib * nib = [UINib nibWithNibName:[self nibName]
bundle:classBundle];
return nib;
}
@end
Now, when you extend this base class, always implement the following:
// MUST OVERRIDE THIS IN .M FILE
+ (NSString *)cellIdentifier {
static NSString* _cellIdentifier = @"UXRHeaderCollectionReuseableView";
_cellIdentifier = NSStringFromClass([self class]);
return _cellIdentifier;
}
You are now free to create a .xib file of your UICollectionReusableView called “UXRHeaderCollectionReuseableView.xib,” name it “UXRHeaderCollectionReuseableView” and set it’s class to “UXRHeaderCollectionReuseableView.”
When you want to use this custom header file, use the following method in your parent collection view:
#pragma mark - Collection Header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
self.headerView = [UXRHeaderCollectionReuseableView collectionReusableViewForCollectionView:collectionView forIndexPath:indexPath withKind:kind];
reusableview = self.headerView;
}
return reusableview;
}
Note: You might have to check the “enable header” button in the storyboard of the originating UICollectionViewController to make sure this function is called.