[ASPagerNode] Ensure delegate property can be set before the view is loaded and is not overwritten.

This commit is contained in:
Scott Goodson
2015-12-29 20:46:42 -08:00
parent 0dc017918d
commit 1870208153
3 changed files with 24 additions and 38 deletions

View File

@@ -26,13 +26,14 @@
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)flowLayout; - (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)flowLayout;
// The underlying ASCollectionView object. // The underlying ASCollectionView object.
- (ASCollectionView *)collectionView; @property (nonatomic, readonly) ASCollectionView *view;
// Delegate is optional, and uses the same protocol as ASCollectionNode. // Delegate is optional, and uses the same protocol as ASCollectionNode.
// This includes UIScrollViewDelegate as well as most methods from UICollectionViewDelegate, like willDisplay... // This includes UIScrollViewDelegate as well as most methods from UICollectionViewDelegate, like willDisplay...
@property (weak, nonatomic) id <ASCollectionDelegate> delegate; @property (nonatomic, weak) id <ASCollectionDelegate> delegate;
// Data Source is required, and uses a different protocol from ASCollectionNode. // Data Source is required, and uses a different protocol from ASCollectionNode.
//@property (nonatomic, weak) id <ASPagerNodeDataSource> dataSource;
- (void)setDataSource:(id <ASPagerNodeDataSource>)dataSource; - (void)setDataSource:(id <ASPagerNodeDataSource>)dataSource;
- (id <ASPagerNodeDataSource>)dataSource; - (id <ASPagerNodeDataSource>)dataSource;

View File

@@ -19,7 +19,7 @@
@end @end
@implementation ASPagerNode @implementation ASPagerNode
@dynamic delegate; @dynamic view, delegate, dataSource;
- (instancetype)init - (instancetype)init
{ {
@@ -31,6 +31,12 @@
return [self initWithFlowLayout:flowLayout]; return [self initWithFlowLayout:flowLayout];
} }
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
ASDisplayNodeAssert([layout isKindOfClass:[UICollectionViewFlowLayout class]], @"ASPagerNode requires a flow layout.");
return [self initWithFlowLayout:(UICollectionViewFlowLayout *)layout];
}
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)flowLayout - (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)flowLayout
{ {
self = [super initWithCollectionViewLayout:flowLayout]; self = [super initWithCollectionViewLayout:flowLayout];
@@ -40,11 +46,6 @@
return self; return self;
} }
- (ASCollectionView *)collectionView
{
return self.view;
}
- (void)setDataSource:(id <ASPagerNodeDataSource>)pagerDataSource - (void)setDataSource:(id <ASPagerNodeDataSource>)pagerDataSource
{ {
if (pagerDataSource != _pagerDataSource) { if (pagerDataSource != _pagerDataSource) {
@@ -69,8 +70,6 @@
[super didLoad]; [super didLoad];
ASCollectionView *cv = self.view; ASCollectionView *cv = self.view;
cv.asyncDataSource = self;
cv.asyncDelegate = self;
cv.pagingEnabled = YES; cv.pagingEnabled = YES;
cv.allowsSelection = NO; cv.allowsSelection = NO;

View File

@@ -10,14 +10,12 @@
*/ */
#import <AsyncDisplayKit/AsyncDisplayKit.h> #import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASAssert.h>
#import "ViewController.h" #import "ViewController.h"
#import "GradientTableNode.h" #import "GradientTableNode.h"
@interface ViewController () <ASCollectionViewDataSource, ASCollectionViewDelegate> @interface ViewController () <ASPagerNodeDataSource>
{ {
ASCollectionView *_pagerView; ASPagerNode *_pagerNode;
} }
@end @end
@@ -31,23 +29,12 @@
{ {
if (!(self = [super init])) if (!(self = [super init]))
return nil; return nil;
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// flowLayout.itemSize = [[UIScreen mainScreen] bounds].size;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
_pagerView = [[ASCollectionView alloc] initWithCollectionViewLayout:flowLayout]; _pagerNode = [[ASPagerNode alloc] init];
_pagerNode.dataSource = self;
ASRangeTuningParameters rangeTuningParameters; // Could implement ASCollectionDelegate if we wanted extra callbacks, like from UIScrollView.
rangeTuningParameters.leadingBufferScreenfuls = 1.0; //_pagerNode.delegate = self;
rangeTuningParameters.trailingBufferScreenfuls = 1.0;
[_pagerView setTuningParameters:rangeTuningParameters forRangeType:ASLayoutRangeTypeRender];
_pagerView.pagingEnabled = YES;
_pagerView.asyncDataSource = self;
_pagerView.asyncDelegate = self;
self.title = @"Paging Table Nodes"; self.title = @"Paging Table Nodes";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo
@@ -59,20 +46,19 @@
- (void)reloadEverything - (void)reloadEverything
{ {
[_pagerView reloadData]; [_pagerNode reloadData];
} }
- (void)viewDidLoad - (void)viewDidLoad
{ {
[super viewDidLoad]; [super viewDidLoad];
[self.view addSubview:_pagerView]; [self.view addSubnode:_pagerNode];
} }
- (void)viewWillLayoutSubviews - (void)viewWillLayoutSubviews
{ {
_pagerView.frame = self.view.bounds; _pagerNode.frame = self.view.bounds;
_pagerView.contentInset = UIEdgeInsetsZero;
} }
- (BOOL)prefersStatusBarHidden - (BOOL)prefersStatusBarHidden
@@ -81,20 +67,20 @@
} }
#pragma mark - #pragma mark -
#pragma mark ASTableView. #pragma mark ASPagerNode.
- (ASCellNode *)collectionView:(ASCollectionView *)collectionView nodeForItemAtIndexPath:(NSIndexPath *)indexPath; - (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index;
{ {
CGSize boundsSize = collectionView.bounds.size; CGSize boundsSize = pagerNode.bounds.size;
CGSize gradientRowSize = CGSizeMake(boundsSize.width, 100); CGSize gradientRowSize = CGSizeMake(boundsSize.width, 100);
GradientTableNode *node = [[GradientTableNode alloc] initWithElementSize:gradientRowSize]; GradientTableNode *node = [[GradientTableNode alloc] initWithElementSize:gradientRowSize];
node.preferredFrameSize = boundsSize; node.preferredFrameSize = boundsSize;
return node; return node;
} }
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section - (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode
{ {
return (section == 0 ? 10 : 0); return 10;
} }
@end @end