Add collectionNode:nodeBlockForSupplementaryElementOfKind:atIndexPath: (#3078)

* Add optional support for nodeBlockForSupplementaryElementOfKind:

* Update example

* Update harder
This commit is contained in:
Adlai Holler
2017-02-24 18:47:01 -08:00
committed by GitHub
parent 9c82ae9284
commit 2fd487b447
5 changed files with 41 additions and 18 deletions

View File

@@ -480,6 +480,15 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForItemAtIndexPath:(NSIndexPath *)indexPath;
/**
* Asks the data source to provide a node-block to display for the given supplementary element in the collection view.
*
* @param collectionNode The sender.
* @param kind The kind of supplementary element.
* @param indexPath The index path of the supplementary element.
*/
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
/**
* Asks the data source to provide a node to display for the given supplementary element in the collection view.
*

View File

@@ -190,6 +190,7 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier";
unsigned int collectionNodeNodeForItem:1;
unsigned int collectionNodeNodeBlockForItem:1;
unsigned int collectionNodeNodeForSupplementaryElement:1;
unsigned int collectionNodeNodeBlockForSupplementaryElement:1;
unsigned int collectionNodeSupplementaryElementKindsInSection:1;
unsigned int numberOfSectionsInCollectionNode:1;
unsigned int collectionNodeNumberOfItemsInSection:1;
@@ -422,6 +423,7 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier";
_asyncDataSourceFlags.collectionNodeNumberOfItemsInSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:numberOfItemsInSection:)];
_asyncDataSourceFlags.collectionNodeContextForSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:contextForSection:)];
_asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement = [_asyncDataSource respondsToSelector:@selector(collectionNode:nodeForSupplementaryElementOfKind:atIndexPath:)];
_asyncDataSourceFlags.collectionNodeNodeBlockForSupplementaryElement = [_asyncDataSource respondsToSelector:@selector(collectionNode:nodeBlockForSupplementaryElementOfKind:atIndexPath:)];
_asyncDataSourceFlags.collectionNodeSupplementaryElementKindsInSection = [_asyncDataSource respondsToSelector:@selector(collectionNode:supplementaryElementKindsInSection:)];
_asyncDataSourceFlags.interop = [_asyncDataSource conformsToProtocol:@protocol(ASCollectionDataSourceInterop)];
@@ -1555,25 +1557,35 @@ static NSString * const kReuseIdentifier = @"_ASCollectionReuseIdentifier";
- (ASCellNodeBlock)dataController:(ASDataController *)dataController supplementaryNodeBlockOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//TODO _asyncDataSource to support supplementary node block and deprecate nodeForSupplementaryElementOfKind
ASCellNode *node = nil;
if (_asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement) {
ASCellNodeBlock nodeBlock = nil;
if (_asyncDataSourceFlags.collectionNodeNodeBlockForSupplementaryElement) {
GET_COLLECTIONNODE_OR_RETURN(collectionNode, ^{ return [[ASCellNode alloc] init]; });
node = [_asyncDataSource collectionNode:collectionNode nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath];
nodeBlock = [_asyncDataSource collectionNode:collectionNode nodeBlockForSupplementaryElementOfKind:kind atIndexPath:indexPath];
ASDisplayNodeAssert(nodeBlock != nil, @"A node block must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath);
} else if (_asyncDataSourceFlags.collectionNodeNodeForSupplementaryElement) {
GET_COLLECTIONNODE_OR_RETURN(collectionNode, ^{ return [[ASCellNode alloc] init]; });
ASCellNode *node = [_asyncDataSource collectionNode:collectionNode nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath];
ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath);
nodeBlock = ^{ return node; };
} else if (_asyncDataSourceFlags.collectionViewNodeForSupplementaryElement) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
node = [_asyncDataSource collectionView:self nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath];
ASCellNode *node = [_asyncDataSource collectionView:self nodeForSupplementaryElementOfKind:kind atIndexPath:indexPath];
ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath);
nodeBlock = ^{ return node; };
#pragma clang diagnostic pop
}
if (node == nil && _asyncDataSourceFlags.interop) {
node = [[ASCellNode alloc] init];
node.shouldUseUIKitCell = YES;
if (nodeBlock == nil) {
BOOL useUIKitCell = _asyncDataSourceFlags.interop;
nodeBlock = ^{
ASCellNode *node = [[ASCellNode alloc] init];
node.shouldUseUIKitCell = useUIKitCell;
return node;
};
}
ASDisplayNodeAssert(node != nil, @"A node must be returned for supplementary element of kind '%@' at index path '%@'", kind, indexPath);
return ^{ return node; };
return nodeBlock;
}
- (NSArray<NSString *> *)dataController:(ASDataController *)dataController supplementaryNodeKindsInSections:(NSIndexSet *)sections

View File

@@ -16,14 +16,14 @@ NS_ASSUME_NONNULL_BEGIN
@protocol ASSupplementaryNodeSource <NSObject>
/**
* A method to provide the node for the item at the given index.
* A method to provide the node-block for the supplementary element.
*
* @param elementKind The kind of supplementary element.
* @param index The index of the item.
* @return A node for the supplementary element.
* @return A node block for the supplementary element.
* @see collectionNode:nodeForSupplementaryElementOfKind:atIndexPath:
*/
- (ASCellNode *)nodeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index;
- (ASCellNodeBlock)nodeBlockForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index;
@optional

View File

@@ -199,9 +199,9 @@ typedef struct {
}
}
- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
return [[self supplementaryElementSourceForSection:indexPath.section] nodeForSupplementaryElementOfKind:kind atIndex:indexPath.item];
return [[self supplementaryElementSourceForSection:indexPath.section] nodeBlockForSupplementaryElementOfKind:kind atIndex:indexPath.item];
}
- (NSArray<NSString *> *)collectionNode:(ASCollectionNode *)collectionNode supplementaryElementKindsInSection:(NSInteger)section

View File

@@ -96,10 +96,12 @@ ASIGSectionControllerCellForIndexImplementation;
#pragma mark - ASSupplementaryNodeSource
- (ASCellNode *)nodeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index
- (ASCellNodeBlock)nodeBlockForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index
{
ASDisplayNodeAssert([elementKind isEqualToString:UICollectionElementKindSectionHeader], nil);
return [[FeedHeaderNode alloc] init];
return ^{
return [[FeedHeaderNode alloc] init];
};
}
- (ASSizeRange)sizeRangeForSupplementaryElementOfKind:(NSString *)elementKind atIndex:(NSInteger)index