--- title: "ASNodeController (Beta)" layout: docs permalink: /docs/containers-asnodecontroller.html prevPage: containers-asviewcontroller.html nextPage: containers-astablenode.html ---
@implementation PhotoCellNodeController
- (void)loadNode
{
self.node = [[PhotoCellNode alloc] initWithPhotoObject:self.photoModel];
}
- (void)didEnterPreloadState
{
[super didEnterPreloadState];
CommentFeedModel *commentFeedModel = _photoModel.commentFeed;
[commentFeedModel refreshFeedWithCompletionBlock:^(NSArray *newComments) {
// load comments for photo
if (commentFeedModel.numberOfItemsInFeed > 0) {
[self.node.photoCommentsNode updateWithCommentFeedModel:commentFeedModel];
[self.node setNeedsLayout];
}
}];
}
@end
@implementation PhotoFeedNodeController
{
PhotoFeedModel *_photoFeed;
ASTableNode *_tableNode;
NSMutableArray *_photoCellNodeControllers;
}
- (instancetype)init
{
_tableNode = [[ASTableNode alloc] init];
self = [super initWithNode:_tableNode];
if (self) {
self.navigationItem.title = @"Texture";
[self.navigationController setNavigationBarHidden:YES];
_tableNode.dataSource = self;
_tableNode.delegate = self;
_photoCellNodeControllers = [NSMutableArray array];
}
return self;
}
- (void)insertNewRowsInTableNode:(NSArray *)newPhotos
{
NSInteger section = 0;
NSMutableArray *indexPaths = [NSMutableArray array];
NSUInteger newTotalNumberOfPhotos = [_photoFeed numberOfItemsInFeed];
for (NSUInteger row = newTotalNumberOfPhotos - newPhotos.count; row < newTotalNumberOfPhotos; row++) {
// create photoCellNodeControllers for the new photos
PhotoCellNodeController *cellController = [[PhotoCellNodeController alloc] init];
cellController.photoModel = [_photoFeed objectAtIndex:row];
[_photoCellNodeControllers addObject:cellController];
// include this index path in the insert rows call for the table
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
[indexPaths addObject:path];
}
[_tableNode insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCellNodeController *cellController = [_photoCellNodeControllers objectAtIndex:indexPath.row];
// this will be executed on a background thread - important to make sure it's thread safe
ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
PhotoCellNode *cellNode = [cellController node];
return cellNode;
};
return ASCellNodeBlock;
}