Files
AsyncDisplayKit/_docs/containers-astablenode.md
Hannah Troisi d162141e18 # This is a combination of 44 commits.
# The first commit's message is:
# This is a combination of 30 commits.
# The first commit's message is:
Update nav_docs.yml

# This is the 2nd commit message:

Create node-containers-overview.md

# This is the 3rd commit message:

Update node-containers-overview.md

# This is the 4th commit message:

Update node-containers-overview.md

# This is the 5th commit message:

Update node-containers-overview.md

# This is the 6th commit message:

Update node-containers-overview.md

# This is the 7th commit message:

Update nav_docs.yml

# This is the 8th commit message:

Update nav_docs.yml

# This is the 9th commit message:

Update and rename node-containers-overview.md to containers-overview.md

# This is the 10th commit message:

Update and rename asviewcontroller.md to container-asviewcontroller.md

# This is the 11th commit message:

Update and rename astablenode.md to container-astablenode.md

# This is the 12th commit message:

Update and rename ascollectionnode.md to container-ascollectionnode.md

# This is the 13th commit message:

Update and rename aspagernode.md to container-aspagernode.md

# This is the 14th commit message:

Update and rename container-ascollectionnode.md to containers-ascollectionnode.md

# This is the 15th commit message:

Update and rename container-asviewcontroller.md to containers-asviewcontroller.md

# This is the 16th commit message:

Update containers-ascollectionnode.md

# This is the 17th commit message:

Update container-astablenode.md

# This is the 18th commit message:

Rename container-astablenode.md to containers-astablenode.md

# This is the 19th commit message:

Update and rename container-aspagernode.md to containers-aspagernode.md

# This is the 20th commit message:

Update containers-overview.md

# This is the 21st commit message:

Update containers-overview.md

# This is the 22nd commit message:

Update containers-overview.md

# This is the 23rd commit message:

Update containers-asviewcontroller.md

# This is the 24th commit message:

Update containers-asviewcontroller.md

# This is the 25th commit message:

Update containers-overview.md

# This is the 26th commit message:

Update containers-asviewcontroller.md

# This is the 27th commit message:

Update containers-asviewcontroller.md

# This is the 28th commit message:

Update containers-asviewcontroller.md

# This is the 29th commit message:

Update containers-overview.md

# This is the 30th commit message:

Update containers-overview.md

# This is the 2nd commit message:

Removing _site folder from repository (only needed to run locally)

# This is the 3rd commit message:

Update containers-overview.md

# This is the 4th commit message:

Update containers-overview.md

# This is the 5th commit message:

Update containers-asviewcontroller.md

# This is the 6th commit message:

Update debug-tool-pixel-scaling.md

# This is the 7th commit message:

Update debug-tool-pixel-scaling.md

# This is the 8th commit message:

Update debug-tool-pixel-scaling.md

# This is the 9th commit message:

Update debug-tool-pixel-scaling.md

# This is the 10th commit message:

Update debug-tool-pixel-scaling.md

# This is the 11th commit message:

Update debug-tool-pixel-scaling.md

# This is the 12th commit message:

Update debug-tool-pixel-scaling.md

# This is the 13th commit message:

Update debug-tool-hit-test-slop.md

# This is the 14th commit message:

Update debug-tool-hit-test-slop.md

# This is the 15th commit message:

Update nav_docs.yml

# This is the 16th commit message:

Update image-node.md

# This is the 17th commit message:

Update image-node.md

# This is the 18th commit message:

Update control-node.md

# This is the 19th commit message:

Update control-node.md

# This is the 20th commit message:

Update nav_docs.yml

# This is the 21st commit message:

Create batch-fetching-api.md

# This is the 22nd commit message:

Update batch-fetching-api.md

# This is the 23rd commit message:

Update debug-tool-hit-test-slop.md

# This is the 24th commit message:

Update debug-tool-pixel-scaling.md

# This is the 25th commit message:

Update debug-tool-hit-test-slop.md

# This is the 26th commit message:

Update debug-tool-pixel-scaling.md

# This is the 27th commit message:

Update containers-aspagernode.md

# This is the 28th commit message:

Update containers-aspagernode.md

# This is the 29th commit message:

Update containers-aspagernode.md

# This is the 30th commit message:

Update containers-aspagernode.md

# This is the 31st commit message:

Update containers-asviewcontroller.md

# This is the 32nd commit message:

Update containers-asviewcontroller.md

# This is the 33rd commit message:

Update containers-asviewcontroller.md

# This is the 34th commit message:

Update containers-asviewcontroller.md

# This is the 35th commit message:

Update containers-overview.md

# This is the 36th commit message:

Update containers-asviewcontroller.md

# This is the 37th commit message:

Update containers-astablenode.md

# This is the 38th commit message:

Update containers-astablenode.md

# This is the 39th commit message:

Update containers-astablenode.md

# This is the 40th commit message:

Update containers-astablenode.md

# This is the 41st commit message:

Update containers-astablenode.md

# This is the 42nd commit message:

Update containers-astablenode.md

# This is the 43rd commit message:

Update containers-astablenode.md

# This is the 44th commit message:

Update containers-astablenode.md
2016-04-19 00:54:31 -07:00

6.3 KiB
Executable File
Raw Blame History

title, layout, permalink, next
title layout permalink next
ASTableNode docs /docs/containers-astablenode.html containers-ascollectionnode.html

ASTableNode is equivalent to UIKit's UITableView and can be used in place of any UITableView.

ASTableNode replaces UITableView's required method

tableView:cellForRowAtIndexPath:

with your choice of one of the following methods

- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath

or

- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath (recommended)

These two methods, need to return either an ASCellNode or an ASCellNodeBlock. An ASCellNodeBlock is a block that creates a ASCellNode which can be run on a background thread. Note that ASCellNodes are used by ASTableNode, ASCollectionNod and ASPagerNode.

Note that both of these methods should not implement reuse (they will be called once per row). However, unlike UITableView, these methods are not called when the row is just about to display.

####Node Blocks are Best####

While tableView:nodeForRowAtIndexPath: will be called on the main thread, tableView:nodeBlockForRowAtIndexPath: is preferred because it concurrently allocates cell nodes. This means that all of the init: methods for all of your subnodes are run in the background.

##Replace UITableViewController with ASViewController##

AsyncDisplayKit does not offer an equivalent to UITableViewController. Instead, use an ASViewController initialized with an ASTableNode.

Consider, again, the ASViewController subclass - PhotoFeedNodeController - from the ASDKgram sample app that uses a table node as its managed node.

An ASTableNode is assigned to be managed by an ASViewController in its initWithNode: designated initializer method.

- (instancetype)init
{
  _tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
  self = [super initWithNode:_tableNode];
  
  if (self) {
    _tableNode.dataSource = self;
    _tableNode.delegate = self;
  }
  
  return self;
}

##Node Block Thread Safety Warning##

It is very important that node blocks be thread-safe. One aspect of that is ensuring that the data model is accessed outside of the node block. Therefore, it is unlikely that you should need to use the index inside of the block.

Consider the following tableView:nodeBlockForRowAtIndexPath: method from the PhotoFeedNodeController.m file in the ASDKgram sample app.

In the example below, you can see how the index is used to access the photo model before creating the node block.

- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
  PhotoModel *photoModel = [_photoFeed objectAtIndex:indexPath.row];
  
  // this may be executed on a background thread - it is important to make sure it is thread safe
  ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
    PhotoCellNode *cellNode = [[PhotoCellNode alloc] initWithPhotoObject:photoModel];
    cellNode.delegate = self;
    return cellNode;
  };
  
  return ASCellNodeBlock;
}

##Accessing the ASTableView## If you've used previous versions of ASDK, you'll notice that ASTableView has been removed in favor of ASTableNode.

`ASTableView` (an actual UITableView subclass) is still used as an internal property of `ASTableNode`. While it should not be created directly, it can still be used directly by accessing the .view property of an `ASTableNode`.

Do not forget that anything that accesses a view using AsyncDisplayKit's node containers or nodes should be done in viewDidLoad or didLoad, respectively.

For example, you may want to set a table's separator style property. This can be done by accessing the table node's view in the viewDidLoad: method as seen in the example below.

- (void)viewDidLoad
{
  [super viewDidLoad];
  
  _tableNode.view.allowsSelection = NO;
  _tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
  _tableNode.view.leadingScreensForBatching = AUTO_TAIL_LOADING_NUM_SCREENFULS;  // overriding default of 2.0
}

##Table Row Height##

An important thing to notice is that ASTableNode does not provide an equivalent to UITableView's

tableView:heightForRowAtIndexPath:

This is because in AsyncDisplayKit, nodes are responsible for determining their height themselves which means you no longer have to write code to determine this detail at the view controller level.

A node defines its height by way of its layoutSpec returned in the - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize method. All nodes given a constrained size are able to calculate their desired size. Note that nodes that don't have an inherent size, such as an image or map) must set their .preferredFrameSize property.

By default, the size range provided to the cell is the width of the table and zero height (min) and maximum is width of table and infinite height (max).

This is the magic of the ASTableView. From the level of the ASCellNode, the cell can very easily control its height and the tableNode will automatically adjust accordingly. For an example of this in action, see how the ASDKgram sample app inserts comments below comments at a later time, increasing the height magically!

If you call -setNeedsLayout on an ASCellNode, it will automatically be perform its layout measurement again and if its overall desired size has changed, the table or collection will be informed and update. This is different from UIKit where normally you would have to call reload row / item. This saves tons of code, check out the ASDKgram sample app to see side by side implementations of an UITableView and ASTableNode implemented social media feed.

##Sample Apps with ASTableNodes##