From 2eaf0f1890ab90a04029198bcf6a8fc54f2821b3 Mon Sep 17 00:00:00 2001 From: Rawlings Date: Thu, 20 Oct 2016 13:31:00 +0800 Subject: [PATCH 1/3] Support supplementaryViews created from nibs --- Source/IGListAdapter.m | 18 ++++++++++++++++++ Source/IGListCollectionContext.h | 19 +++++++++++++++++++ Source/IGListStackedSectionController.m | 14 ++++++++++++++ Source/Internal/IGListAdapterInternal.h | 1 + 4 files changed, 52 insertions(+) diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 68ce31384..3ecab5b39 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -74,6 +74,7 @@ - (void)setCollectionView:(IGListCollectionView *)collectionView { _registeredCellClasses = [NSMutableSet new]; _registeredNibNames = [NSMutableSet new]; _registeredSupplementaryViewIdentifiers = [NSMutableSet new]; + _registeredSupplementaryViewNibNames = [NSMutableSet new]; _collectionView = collectionView; _collectionView.dataSource = self; @@ -759,6 +760,23 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath]; } +- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind + forSectionController:(IGListSectionController *)sectionController + nibName:(NSString *)nibName + bundle:(NSBundle *)bundle + atIndex:(NSInteger)index { + IGAssertMainThread(); + UICollectionView *collectionView = self.collectionView; + IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); + NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index]; + if (![self.registeredSupplementaryViewNibNames containsObject:nibName]) { + [self.registeredSupplementaryViewNibNames addObject:nibName]; + UINib *nib = [UINib nibWithNibName:nibName bundle:bundle]; + [collectionView registerNib:nib forSupplementaryViewOfKind:elementKind withReuseIdentifier:nibName]; + } + return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:nibName forIndexPath:indexPath]; +} + - (void)reloadInSectionController:(IGListSectionController *)sectionController atIndexes:(NSIndexSet *)indexes { IGAssertMainThread(); IGParameterAssert(indexes != nil); diff --git a/Source/IGListCollectionContext.h b/Source/IGListCollectionContext.h index aeaf8db88..58d40474b 100644 --- a/Source/IGListCollectionContext.h +++ b/Source/IGListCollectionContext.h @@ -125,6 +125,25 @@ NS_ASSUME_NONNULL_BEGIN class:(Class)viewClass atIndex:(NSInteger)index; +/** + Dequeues a supplementary view from the UICollectionView reuse pool. + + @param elementKind The kind of supplementary veiw. + @param sectionController The section controller requesting this information. + @param nibName The name of the nib file. + @param bundle The bundle in which to search for the nib file. If nil, this method looks for the nib file in the main bundle. + @param index The index of the supplementary vew. + + @return A supplementary view dequeued from the reuse pool or newly created. + + @note This method uses a string representation of the view class as the identifier. + */ +- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind + forSectionController:(IGListSectionController *)sectionController + nibName:(NSString *)nibName + bundle:(NSBundle *)bundle + atIndex:(NSInteger)index; + /** Reloads cells in the section controller. diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index c3b3a9139..7d162e7be 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -219,6 +219,20 @@ - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString * atIndex:(index + offset)]; } +- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind + forSectionController:(IGListSectionController *)sectionController + nibName:(NSString *)nibName + bundle:(NSBundle *)bundle + atIndex:(NSInteger)index +{ + const NSUInteger offset = [self offsetForSectionController:sectionController]; + return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind + forSectionController:self + nibName:nibName + bundle:bundle + atIndex:(index + offset)]; +} + - (void)reloadInSectionController:(IGListSectionController *)sectionController atIndexes:(NSIndexSet *)indexes { NSIndexSet *itemIndexes = [self itemIndexesForSectionController:sectionController indexes:indexes]; [self.collectionContext reloadInSectionController:self atIndexes:itemIndexes]; diff --git a/Source/Internal/IGListAdapterInternal.h b/Source/Internal/IGListAdapterInternal.h index d14dca8f2..fc1def348 100644 --- a/Source/Internal/IGListAdapterInternal.h +++ b/Source/Internal/IGListAdapterInternal.h @@ -56,6 +56,7 @@ IGListCollectionContext @property (nonatomic, strong) NSMutableSet *registeredCellClasses; @property (nonatomic, strong) NSMutableSet *registeredNibNames; @property (nonatomic, strong) NSMutableSet *registeredSupplementaryViewIdentifiers; +@property (nonatomic, strong) NSMutableSet *registeredSupplementaryViewNibNames; - (NSArray *)indexPathsFromSectionController:(IGListSectionController *)sectionController indexes:(NSIndexSet *)indexes From 7111bb585b1bf7a4d24569f3a1e7748b5061ae33 Mon Sep 17 00:00:00 2001 From: Rawlings Date: Fri, 21 Oct 2016 00:37:14 +0800 Subject: [PATCH 2/3] complete --- CHANGELOG.md | 1 + Source/IGListAdapter.m | 3 +++ Source/IGListCollectionContext.h | 2 +- Source/IGListStackedSectionController.m | 14 +++++++------- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a64f697a9..f1dd31ee7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The changelog for `IGListKit`. Also see the [releases](https://github.com/instag ### Enhancements +- Added support for supplementaryViews created from nibs. [Rawlinxx](https://github.com/rawlinxx) [(#90)](https://github.com/Instagram/IGListKit/pull/90) - Added support for cells created from nibs. [Sven Bacia](https://github.com/svenbacia) [(#56)](https://github.com/Instagram/IGListKit/pull/56) - Added an additional initializer for `IGListSingleSectionController` to be able to support single sections created from nibs. An example can be found [here](Example/IGListKitExamples/ViewControllers/SingleSectionViewController.swift). - Fixed `-[IGListAdapter reloadDataWithCompletion:]` not returning early when `collectionView` or `dataSource` is nil and `completion` is nil. [Ben Asher](https://github.com/benasher44) [(#51)](https://github.com/Instagram/IGListKit/pull/51) diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 3ecab5b39..363a37cab 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -732,6 +732,7 @@ - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { IGAssertMainThread(); + IGParameterAssert([nibName length] > 0); IGParameterAssert(sectionController != nil); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); @@ -766,6 +767,8 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N bundle:(NSBundle *)bundle atIndex:(NSInteger)index { IGAssertMainThread(); + IGParameterAssert([nibName length] > 0); + IGParameterAssert([elementKind length] > 0); UICollectionView *collectionView = self.collectionView; IGAssert(collectionView != nil, @"Reloading adapter without a collection view."); NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index]; diff --git a/Source/IGListCollectionContext.h b/Source/IGListCollectionContext.h index 58d40474b..7b850ebcf 100644 --- a/Source/IGListCollectionContext.h +++ b/Source/IGListCollectionContext.h @@ -141,7 +141,7 @@ NS_ASSUME_NONNULL_BEGIN - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind forSectionController:(IGListSectionController *)sectionController nibName:(NSString *)nibName - bundle:(NSBundle *)bundle + bundle:(nullable NSBundle *)bundle atIndex:(NSInteger)index; /** diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index 7d162e7be..2a6e93449 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -213,20 +213,20 @@ - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString * class:(Class)viewClass atIndex:(NSInteger)index { const NSUInteger offset = [self offsetForSectionController:sectionController]; - return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind + return (UICollectionReusableView *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self class:viewClass atIndex:(index + offset)]; } -- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind - forSectionController:(IGListSectionController *)sectionController - nibName:(NSString *)nibName - bundle:(NSBundle *)bundle - atIndex:(NSInteger)index +- (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind + forSectionController:(IGListSectionController *)sectionController + nibName:(NSString *)nibName + bundle:(NSBundle *)bundle + atIndex:(NSInteger)index { const NSUInteger offset = [self offsetForSectionController:sectionController]; - return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind + return (UICollectionReusableView *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self nibName:nibName bundle:bundle From 1dbabbb0c0259951f86e9c27c68a7959c29b21f9 Mon Sep 17 00:00:00 2001 From: Rawlings Date: Fri, 21 Oct 2016 18:48:25 +0800 Subject: [PATCH 3/3] complete --- Source/IGListAdapter.m | 6 +++--- Source/IGListCollectionContext.h | 2 +- Source/IGListStackedSectionController.m | 12 +++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Source/IGListAdapter.m b/Source/IGListAdapter.m index 363a37cab..f0f482b0b 100644 --- a/Source/IGListAdapter.m +++ b/Source/IGListAdapter.m @@ -711,7 +711,7 @@ - (void)deselectItemAtIndex:(NSInteger)index } - (__kindof UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass - forSectionController:(IGListSectionController *)sectionController + forSectionController:(IGListSectionController *)sectionController atIndex:(NSInteger)index { IGAssertMainThread(); IGParameterAssert(sectionController != nil); @@ -746,7 +746,7 @@ - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName } - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind - forSectionController:(IGListSectionController *)sectionController + forSectionController:(IGListSectionController *)sectionController class:(Class)viewClass atIndex:(NSInteger)index { IGAssertMainThread(); @@ -762,7 +762,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N } - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind - forSectionController:(IGListSectionController *)sectionController + forSectionController:(IGListSectionController *)sectionController nibName:(NSString *)nibName bundle:(NSBundle *)bundle atIndex:(NSInteger)index { diff --git a/Source/IGListCollectionContext.h b/Source/IGListCollectionContext.h index 7b850ebcf..1805729bd 100644 --- a/Source/IGListCollectionContext.h +++ b/Source/IGListCollectionContext.h @@ -139,7 +139,7 @@ NS_ASSUME_NONNULL_BEGIN @note This method uses a string representation of the view class as the identifier. */ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind - forSectionController:(IGListSectionController *)sectionController + forSectionController:(IGListSectionController *)sectionController nibName:(NSString *)nibName bundle:(nullable NSBundle *)bundle atIndex:(NSInteger)index; diff --git a/Source/IGListStackedSectionController.m b/Source/IGListStackedSectionController.m index 2a6e93449..ccdccc7ad 100644 --- a/Source/IGListStackedSectionController.m +++ b/Source/IGListStackedSectionController.m @@ -199,8 +199,7 @@ - (UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle forSectionController:(IGListSectionController *)sectionController - atIndex:(NSInteger)index -{ + atIndex:(NSInteger)index { const NSUInteger offset = [self offsetForSectionController:sectionController]; return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableCellWithNibName:nibName bundle:bundle @@ -213,20 +212,19 @@ - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString * class:(Class)viewClass atIndex:(NSInteger)index { const NSUInteger offset = [self offsetForSectionController:sectionController]; - return (UICollectionReusableView *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind + return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self class:viewClass atIndex:(index + offset)]; } - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind - forSectionController:(IGListSectionController *)sectionController + forSectionController:(IGListSectionController *)sectionController nibName:(NSString *)nibName bundle:(NSBundle *)bundle - atIndex:(NSInteger)index -{ + atIndex:(NSInteger)index { const NSUInteger offset = [self offsetForSectionController:sectionController]; - return (UICollectionReusableView *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind + return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind forSectionController:self nibName:nibName bundle:bundle