Skip to content

Commit

Permalink
[Example] Migrate to the recommended -nodeBlock data source method,…
Browse files Browse the repository at this point in the history
… so node containers will be able to prepare and display all of its cells concurrently
  • Loading branch information
hanton committed Jul 25, 2019
1 parent 9cedd4e commit 088f46b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 37 deletions.
10 changes: 5 additions & 5 deletions examples/ASDKTube/Sample/Controller/VideoFeedNodeController.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ - (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ class ViewController: ASViewController<ASCollectionNode>, 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)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ - (void)reloadEverything

#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
Expand Down
17 changes: 10 additions & 7 deletions examples/Kittens/Sample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,20 @@ - (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
8 changes: 5 additions & 3 deletions examples/LayoutSpecExamples/Sample/OverviewViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ - (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger
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
Expand Down
23 changes: 13 additions & 10 deletions examples/Swift/Sample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ final class ViewController: ASViewController<ASDisplayNode>, 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ - (void)viewWillLayoutSubviews
#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;
Expand Down

0 comments on commit 088f46b

Please sign in to comment.