Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support supplementaryViews created from nibs #90

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This release closes the [2.0.0 milestone](https://github.com/Instagram/IGListKit

### 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)
Expand Down
26 changes: 23 additions & 3 deletions Source/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -710,7 +711,7 @@ - (void)deselectItemAtIndex:(NSInteger)index
}

- (__kindof UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass
forSectionController:(IGListSectionController <IGListSectionType> *)sectionController
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
atIndex:(NSInteger)index {
IGAssertMainThread();
IGParameterAssert(sectionController != nil);
Expand All @@ -732,7 +733,7 @@ - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
atIndex:(NSInteger)index {
IGAssertMainThread();
IGParameterAssert(nibName != nil);
IGParameterAssert([nibName length] > 0);
IGParameterAssert(sectionController != nil);
IGParameterAssert(index >= 0);
UICollectionView *collectionView = self.collectionView;
Expand All @@ -747,7 +748,7 @@ - (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName
}

- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
forSectionController:(IGListSectionController <IGListSectionType> *)sectionController
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
class:(Class)viewClass
atIndex:(NSInteger)index {
IGAssertMainThread();
Expand All @@ -766,6 +767,25 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N
return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
}

- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
nibName:(NSString *)nibName
bundle:(NSBundle *)bundle
atIndex:(NSInteger)index {
IGAssertMainThread();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add:

IGParameterAssert([nibName length] > 0);
IGParameterAssert([elementKind length] > 0);

I realize those are also missing from other methods. Adding some starter tasks for that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bundle should be nullable here, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that, I'm trying this framework, hope to have this feature ASAP, without attention to details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rawlinxx no worries! PRs are an iterative process, its good to get something up and we can all work on it together 👨‍👩‍👧‍👦

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];
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<IGListSectionType> *)sectionController atIndexes:(NSIndexSet *)indexes {
IGAssertMainThread();
IGParameterAssert(indexes != nil);
Expand Down
19 changes: 19 additions & 0 deletions Source/IGListCollectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGListSectionType> *)sectionController
nibName:(NSString *)nibName
bundle:(nullable NSBundle *)bundle
atIndex:(NSInteger)index;

/**
Reloads cells in the section controller.

Expand Down
16 changes: 14 additions & 2 deletions Source/IGListStackedSectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ - (UICollectionViewCell *)dequeueReusableCellOfClass:(Class)cellClass
- (UICollectionViewCell *)dequeueReusableCellWithNibName:(NSString *)nibName
bundle:(NSBundle *)bundle
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
atIndex:(NSInteger)index
{
atIndex:(NSInteger)index {
const NSUInteger offset = [self offsetForSectionController:sectionController];
return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableCellWithNibName:nibName
bundle:bundle
Expand All @@ -219,6 +218,19 @@ - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *
atIndex:(index + offset)];
}

- (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
forSectionController:(IGListSectionController<IGListSectionType> *)sectionController
nibName:(NSString *)nibName
bundle:(NSBundle *)bundle
atIndex:(NSInteger)index {
const NSUInteger offset = [self offsetForSectionController:sectionController];
return (UICollectionViewCell *_Nonnull)[self.collectionContext dequeueReusableSupplementaryViewOfKind:elementKind
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this cast?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not quite understand why (UICollectionViewCell *_Nonnull) is needed here, just following the previous method implementation since I have not read all the source code yet, i think UICollectionReusableView is more suitable here, am i wrong?

forSectionController:self
nibName:nibName
bundle:bundle
atIndex:(index + offset)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we realign the colons?

}

- (void)reloadInSectionController:(IGListSectionController<IGListSectionType> *)sectionController atIndexes:(NSIndexSet *)indexes {
NSIndexSet *itemIndexes = [self itemIndexesForSectionController:sectionController indexes:indexes];
[self.collectionContext reloadInSectionController:self atIndexes:itemIndexes];
Expand Down
1 change: 1 addition & 0 deletions Source/Internal/IGListAdapterInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ IGListCollectionContext
@property (nonatomic, strong) NSMutableSet<Class> *registeredCellClasses;
@property (nonatomic, strong) NSMutableSet<NSString *> *registeredNibNames;
@property (nonatomic, strong) NSMutableSet<NSString *> *registeredSupplementaryViewIdentifiers;
@property (nonatomic, strong) NSMutableSet<NSString *> *registeredSupplementaryViewNibNames;

- (NSArray *)indexPathsFromSectionController:(IGListSectionController <IGListSectionType> *)sectionController
indexes:(NSIndexSet *)indexes
Expand Down