From 4a2d77dc9e896eaa17e1eb83f234c69ae2dd6eae Mon Sep 17 00:00:00 2001 From: Hanton Date: Mon, 29 Jul 2019 04:08:28 +0800 Subject: [PATCH] [Examples] Migrate to the recommended `-nodeBlock` data source method, so node containers will be able to prepare and display all of its cells concurrently (#1596) --- .../Controller/VideoFeedNodeController.m | 10 ++++----- .../Sample/ViewController.swift | 6 ++++-- .../Sample/ViewController.m | 7 ++++--- examples/Kittens/Sample/ViewController.m | 15 +++++++------ .../Sample/OverviewViewController.swift | 7 +++++-- .../Sample/OverviewViewController.m | 8 ++++--- examples/Swift/Sample/ViewController.swift | 21 +++++++++++-------- .../Sample/ViewController.m | 11 +++++----- 8 files changed, 50 insertions(+), 35 deletions(-) diff --git a/examples/ASDKTube/Sample/Controller/VideoFeedNodeController.m b/examples/ASDKTube/Sample/Controller/VideoFeedNodeController.m index b6e53ba7..f05c9d7c 100644 --- a/examples/ASDKTube/Sample/Controller/VideoFeedNodeController.m +++ b/examples/ASDKTube/Sample/Controller/VideoFeedNodeController.m @@ -66,12 +66,12 @@ return _videoFeedData.count; } -- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath -{ +- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { VideoModel *videoObject = [_videoFeedData objectAtIndex:indexPath.row]; - VideoContentCell *cellNode = [[VideoContentCell alloc] initWithVideoObject:videoObject]; - - return cellNode; + return ^{ + VideoContentCell *cellNode = [[VideoContentCell alloc] initWithVideoObject:videoObject]; + return cellNode; + }; } @end diff --git a/examples/CustomCollectionView-Swift/Sample/ViewController.swift b/examples/CustomCollectionView-Swift/Sample/ViewController.swift index 683e7e24..3af62907 100644 --- a/examples/CustomCollectionView-Swift/Sample/ViewController.swift +++ b/examples/CustomCollectionView-Swift/Sample/ViewController.swift @@ -52,9 +52,11 @@ class ViewController: ASViewController, MosaicCollectionViewLa _collectionNode.view.isScrollEnabled = true } - func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode { + func collectionNode(_ collectionNode: ASCollectionNode, nodeBlockForItemAt indexPath: IndexPath) -> ASCellNodeBlock { let image = _sections[indexPath.section][indexPath.item] - return ImageCellNode(with: image) + return { + return ImageCellNode(with: image) + } } diff --git a/examples/HorizontalWithinVerticalScrolling/Sample/ViewController.m b/examples/HorizontalWithinVerticalScrolling/Sample/ViewController.m index f1b18588..5ef9fffc 100644 --- a/examples/HorizontalWithinVerticalScrolling/Sample/ViewController.m +++ b/examples/HorizontalWithinVerticalScrolling/Sample/ViewController.m @@ -55,9 +55,10 @@ #pragma mark - ASTableNode -- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath -{ - return [[HorizontalScrollCellNode alloc] initWithElementSize:CGSizeMake(100, 100)]; +- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { + return ^{ + return [[HorizontalScrollCellNode alloc] initWithElementSize:CGSizeMake(100, 100)]; + }; } - (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section diff --git a/examples/Kittens/Sample/ViewController.m b/examples/Kittens/Sample/ViewController.m index 552f4828..761c21ed 100644 --- a/examples/Kittens/Sample/ViewController.m +++ b/examples/Kittens/Sample/ViewController.m @@ -100,17 +100,20 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell return 1 + _kittenDataSource.count; } -- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath -{ +- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { // special-case the first row if ([_blurbNodeIndexPath compare:indexPath] == NSOrderedSame) { - BlurbNode *node = [[BlurbNode alloc] init]; - return node; + return ^{ + BlurbNode *node = [[BlurbNode alloc] init]; + return node; + }; } NSValue *size = _kittenDataSource[indexPath.row - 1]; - KittenNode *node = [[KittenNode alloc] initWithKittenOfSize:size.CGSizeValue]; - return node; + return ^{ + KittenNode *node = [[KittenNode alloc] initWithKittenOfSize:size.CGSizeValue]; + return node; + }; } - (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath diff --git a/examples/LayoutSpecExamples-Swift/Sample/OverviewViewController.swift b/examples/LayoutSpecExamples-Swift/Sample/OverviewViewController.swift index c43856f1..f306a12f 100644 --- a/examples/LayoutSpecExamples-Swift/Sample/OverviewViewController.swift +++ b/examples/LayoutSpecExamples-Swift/Sample/OverviewViewController.swift @@ -50,8 +50,11 @@ extension OverviewViewController: ASTableDataSource { return layoutExamples.count } - func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode { - return OverviewCellNode(layoutExampleType: layoutExamples[indexPath.row]) + func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { + let layoutExample = layoutExamples[indexPath.row] + return { + return OverviewCellNode(layoutExampleType: layoutExample) + } } } diff --git a/examples/LayoutSpecExamples/Sample/OverviewViewController.m b/examples/LayoutSpecExamples/Sample/OverviewViewController.m index 9d9700f5..99367424 100644 --- a/examples/LayoutSpecExamples/Sample/OverviewViewController.m +++ b/examples/LayoutSpecExamples/Sample/OverviewViewController.m @@ -62,9 +62,11 @@ return [_layoutExamples count]; } -- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath -{ - return [[OverviewCellNode alloc] initWithLayoutExampleClass:_layoutExamples[indexPath.row]]; +- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath { + Class layoutExample = _layoutExamples[indexPath.row]; + return ^{ + return [[OverviewCellNode alloc] initWithLayoutExampleClass:layoutExample]; + }; } - (void)tableNode:(ASTableNode *)tableNode didSelectRowAtIndexPath:(NSIndexPath *)indexPath diff --git a/examples/Swift/Sample/ViewController.swift b/examples/Swift/Sample/ViewController.swift index e68c178a..0c94d86a 100644 --- a/examples/Swift/Sample/ViewController.swift +++ b/examples/Swift/Sample/ViewController.swift @@ -41,21 +41,24 @@ final class ViewController: ASViewController, ASTableDataSource, // MARK: ASTableNode data source and delegate. - func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode { + func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { // Should read the row count directly from table view but // https://github.com/facebook/AsyncDisplayKit/issues/1159 let rowCount = self.tableNode(tableNode, numberOfRowsInSection: 0) if state.fetchingMore && indexPath.row == rowCount - 1 { - let node = TailLoadingCellNode() - node.style.height = ASDimensionMake(44.0) - return node; + return { + let node = TailLoadingCellNode() + node.style.height = ASDimensionMake(44.0) + return node; + } + } + + return { + let node = ASTextCellNode() + node.text = String(format: "[%ld.%ld] says hello!", indexPath.section, indexPath.row) + return node } - - let node = ASTextCellNode() - node.text = String(format: "[%ld.%ld] says hello!", indexPath.section, indexPath.row) - - return node } func numberOfSections(in tableNode: ASTableNode) -> Int { diff --git a/examples/VerticalWithinHorizontalScrolling/Sample/ViewController.m b/examples/VerticalWithinHorizontalScrolling/Sample/ViewController.m index 7e852414..d306a38e 100644 --- a/examples/VerticalWithinHorizontalScrolling/Sample/ViewController.m +++ b/examples/VerticalWithinHorizontalScrolling/Sample/ViewController.m @@ -64,13 +64,14 @@ #pragma mark - #pragma mark ASPagerNode. -- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index -{ +- (ASCellNodeBlock)pagerNode:(ASPagerNode *)pagerNode nodeBlockAtIndex:(NSInteger)index { CGSize boundsSize = pagerNode.bounds.size; CGSize gradientRowSize = CGSizeMake(boundsSize.width, 100); - GradientTableNode *node = [[GradientTableNode alloc] initWithElementSize:gradientRowSize]; - node.pageNumber = index; - return node; + return ^{ + GradientTableNode *node = [[GradientTableNode alloc] initWithElementSize:gradientRowSize]; + node.pageNumber = index; + return node; + }; } - (ASSizeRange)pagerNode:(ASPagerNode *)pagerNode constrainedSizeForNodeAtIndex:(NSInteger)index;