From 7ece41ff645b9ee9e132b06cbc2993f3f8f84b5b Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Thu, 24 Dec 2015 21:41:12 -0800 Subject: [PATCH] Delegate definition tweaks for Table and Collection; ensure Table tests run with ARC enabled. --- AsyncDisplayKit.xcodeproj/project.pbxproj | 2 +- AsyncDisplayKit/ASCollectionView.h | 18 +++++++--------- AsyncDisplayKit/ASPagerNode.h | 21 ++++++++++--------- AsyncDisplayKit/ASPagerNode.m | 10 +++++++-- AsyncDisplayKit/ASTableNode.h | 3 ++- AsyncDisplayKit/ASTableNode.m | 18 +++++++++++++--- AsyncDisplayKit/ASTableView.h | 21 +++++++------------ AsyncDisplayKit/ASTableView.mm | 20 +++++++++++++----- .../ASCollectionViewFlowLayoutInspector.h | 4 ++-- .../ASCollectionViewFlowLayoutInspector.m | 2 +- AsyncDisplayKitTests/ASTableViewTests.m | 14 +++++++------ 11 files changed, 79 insertions(+), 54 deletions(-) diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index 136a89cb43..02830223a8 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -242,7 +242,7 @@ 34EFC7771B701D2D00AD841F /* ASStackUnpositionedLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = ACF6ED491B17847A00DA7C62 /* ASStackUnpositionedLayout.h */; }; 34EFC7781B701D3100AD841F /* ASStackUnpositionedLayout.mm in Sources */ = {isa = PBXBuildFile; fileRef = ACF6ED4A1B17847A00DA7C62 /* ASStackUnpositionedLayout.mm */; }; 34EFC7791B701D3600AD841F /* ASLayoutSpecUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = ACF6ED451B17847A00DA7C62 /* ASLayoutSpecUtilities.h */; }; - 3C9C128519E616EF00E942A0 /* ASTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 3C9C128519E616EF00E942A0 /* ASTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.m */; }; 430E7C8F1B4C23F100697A4C /* ASIndexPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 430E7C8D1B4C23F100697A4C /* ASIndexPath.h */; settings = {ATTRIBUTES = (Public, ); }; }; 430E7C901B4C23F100697A4C /* ASIndexPath.h in Headers */ = {isa = PBXBuildFile; fileRef = 430E7C8D1B4C23F100697A4C /* ASIndexPath.h */; settings = {ATTRIBUTES = (Public, ); }; }; 430E7C911B4C23F100697A4C /* ASIndexPath.m in Sources */ = {isa = PBXBuildFile; fileRef = 430E7C8E1B4C23F100697A4C /* ASIndexPath.m */; }; diff --git a/AsyncDisplayKit/ASCollectionView.h b/AsyncDisplayKit/ASCollectionView.h index b0072e64cf..f7cd8da049 100644 --- a/AsyncDisplayKit/ASCollectionView.h +++ b/AsyncDisplayKit/ASCollectionView.h @@ -15,8 +15,8 @@ #import @class ASCellNode; -@protocol ASCollectionViewDataSource; -@protocol ASCollectionViewDelegate; +@protocol ASCollectionDataSource; +@protocol ASCollectionDelegate; @protocol ASCollectionViewLayoutInspecting; /** @@ -35,8 +35,8 @@ - (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout; - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout; -@property (nonatomic, weak) id asyncDataSource; -@property (nonatomic, weak) id asyncDelegate; // must not be nil +@property (nonatomic, weak) id asyncDelegate; +@property (nonatomic, weak) id asyncDataSource; /** * Tuning parameters for a range type. @@ -286,9 +286,8 @@ /** * This is a node-based UICollectionViewDataSource. */ -@protocol ASCollectionDataSource -@end -@protocol ASCollectionViewDataSource +#define ASCollectionViewDataSource ASCollectionDataSource +@protocol ASCollectionDataSource /** * Similar to -collectionView:cellForItemAtIndexPath:. @@ -349,9 +348,8 @@ /** * This is a node-based UICollectionViewDelegate. */ -@protocol ASCollectionDelegate -@end -@protocol ASCollectionViewDelegate +#define ASCollectionViewDelegate ASCollectionDelegate +@protocol ASCollectionDelegate @optional diff --git a/AsyncDisplayKit/ASPagerNode.h b/AsyncDisplayKit/ASPagerNode.h index f2f69b61f3..325eacea53 100644 --- a/AsyncDisplayKit/ASPagerNode.h +++ b/AsyncDisplayKit/ASPagerNode.h @@ -8,7 +8,17 @@ #import -@protocol ASPagerNodeDataSource; +@class ASPagerNode; + +@protocol ASPagerNodeDataSource + +// This method replaces -collectionView:numberOfItemsInSection: +- (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode; + +// This method replaces -collectionView:nodeForItemAtIndexPath: +- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index; + +@end @interface ASPagerNode : ASCollectionNode @@ -32,12 +42,3 @@ @end -@protocol ASPagerNodeDataSource - -// This method replaces -collectionView:numberOfItemsInSection: -- (NSInteger)numberOfPagesInPagerNode:(ASPagerNode *)pagerNode; - -// This method replaces -collectionView:nodeForItemAtIndexPath: -- (ASCellNode *)pagerNode:(ASPagerNode *)pagerNode nodeAtIndex:(NSInteger)index; - -@end diff --git a/AsyncDisplayKit/ASPagerNode.m b/AsyncDisplayKit/ASPagerNode.m index dbaaa97300..d4537d0a68 100644 --- a/AsyncDisplayKit/ASPagerNode.m +++ b/AsyncDisplayKit/ASPagerNode.m @@ -11,7 +11,7 @@ #import -@interface ASPagerNode () { +@interface ASPagerNode () { UICollectionViewFlowLayout *_flowLayout; ASPagerNodeProxy *_proxy; id _pagerDataSource; @@ -20,6 +20,7 @@ @end @implementation ASPagerNode +@dynamic delegate; - (instancetype)init { @@ -50,10 +51,15 @@ if (pagerDataSource != _pagerDataSource) { _pagerDataSource = pagerDataSource; _proxy = pagerDataSource ? [[ASPagerNodeProxy alloc] initWithTarget:pagerDataSource interceptor:self] : nil; - super.dataSource = _proxy; + super.dataSource = (id )_proxy; } } +- (void)proxyTargetHasDeallocated:(ASDelegateProxy *)proxy +{ + [self setDataSource:nil]; +} + - (id )dataSource { return _pagerDataSource; diff --git a/AsyncDisplayKit/ASTableNode.h b/AsyncDisplayKit/ASTableNode.h index d859df677d..9ab36470a6 100644 --- a/AsyncDisplayKit/ASTableNode.h +++ b/AsyncDisplayKit/ASTableNode.h @@ -14,7 +14,8 @@ */ @interface ASTableNode : ASDisplayNode -- (instancetype)initWithStyle:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER; +- (instancetype)init; // UITableViewStylePlain +- (instancetype)initWithStyle:(UITableViewStyle)style; @property (nonatomic, readonly) ASTableView *view; diff --git a/AsyncDisplayKit/ASTableNode.m b/AsyncDisplayKit/ASTableNode.m index 2d9bbc33bb..f45f742d1e 100644 --- a/AsyncDisplayKit/ASTableNode.m +++ b/AsyncDisplayKit/ASTableNode.m @@ -21,19 +21,31 @@ @end @interface ASTableView () -- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style; +- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass; @end @implementation ASTableNode -- (instancetype)initWithStyle:(UITableViewStyle)style +- (instancetype)_initWithStyle:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass { - if (self = [super initWithViewBlock:^UIView *{ return [[ASTableView alloc] _initWithFrame:CGRectZero style:style]; }]) { + if (self = [super initWithViewBlock:^UIView *{ return [[ASTableView alloc] _initWithFrame:CGRectZero + style:style + dataControllerClass:dataControllerClass]; }]) { return self; } return nil; } +- (instancetype)initWithStyle:(UITableViewStyle)style +{ + return [self _initWithStyle:style dataControllerClass:nil]; +} + +- (instancetype)init +{ + return [self _initWithStyle:UITableViewStylePlain dataControllerClass:nil]; +} + - (void)didLoad { [super didLoad]; diff --git a/AsyncDisplayKit/ASTableView.h b/AsyncDisplayKit/ASTableView.h index f6746874b9..ae43552d03 100644 --- a/AsyncDisplayKit/ASTableView.h +++ b/AsyncDisplayKit/ASTableView.h @@ -7,17 +7,14 @@ */ #import - #import #import #import #import - @class ASCellNode; -@protocol ASTableViewDataSource; -@protocol ASTableViewDelegate; - +@protocol ASTableDataSource; +@protocol ASTableDelegate; /** * Node-based table view. @@ -27,8 +24,8 @@ */ @interface ASTableView : UITableView -@property (nonatomic, weak) id asyncDelegate; // must not be nil -@property (nonatomic, weak) id asyncDataSource; +@property (nonatomic, weak) id asyncDelegate; +@property (nonatomic, weak) id asyncDataSource; /** * Initializer. @@ -280,9 +277,8 @@ /** * This is a node-based UITableViewDataSource. */ -@protocol ASTableDataSource -@end -@protocol ASTableViewDataSource +#define ASTableViewDataSource ASTableDataSource +@protocol ASTableDataSource /** * Similar to -tableView:cellForRowAtIndexPath:. @@ -326,9 +322,8 @@ * Note that -tableView:heightForRowAtIndexPath: has been removed; instead, your custom ASCellNode subclasses are * responsible for deciding their preferred onscreen height in -calculateSizeThatFits:. */ -@protocol ASTableDelegate -@end -@protocol ASTableViewDelegate +#define ASTableViewDelegate ASTableDelegate +@protocol ASTableDelegate @optional diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 162a0fa1b2..a5e65178d7 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -21,6 +21,8 @@ #import "ASLayoutController.h" #import "ASRangeController.h" +#import + static NSString * const kCellReuseIdentifier = @"_ASTableViewCell"; //#define LOG(...) NSLog(__VA_ARGS__) @@ -80,6 +82,10 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell"; #pragma mark - #pragma mark ASTableView +@interface ASTableNode () +- (instancetype)_initWithStyle:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass; +@end + @interface ASTableView () { ASTableViewProxy *_proxyDataSource; ASTableViewProxy *_proxyDelegate; @@ -168,17 +174,21 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell"; - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass asyncDataFetching:(BOOL)asyncDataFetchingEnabled { - ASTableNode *tableNode = [[ASTableNode alloc] initWithStyle:style]; - tableNode.frame = frame; - return tableNode.view; +// ASTableNode *tableNode = [[ASTableNode alloc] _initWithStyle:style dataControllerClass:dataControllerClass]; +// tableNode.frame = frame; +// return tableNode.view; + return [self _initWithFrame:frame style:style dataControllerClass:dataControllerClass]; } -- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style +- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass { if (!(self = [super initWithFrame:frame style:style])) return nil; - [self configureWithDataControllerClass:[self.class dataControllerClass] asyncDataFetching:NO]; + if (!dataControllerClass) { + dataControllerClass = [self.class dataControllerClass]; + } + [self configureWithDataControllerClass:dataControllerClass asyncDataFetching:NO]; return self; } diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h index 2890cab842..b2ee2ae8a8 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.h @@ -11,7 +11,7 @@ #import @class ASCollectionView; -@protocol ASCollectionViewDelegate; +@protocol ASCollectionDelegate; @protocol ASCollectionViewLayoutInspecting @@ -42,7 +42,7 @@ * * @discussion A great time to update perform selector caches! */ -- (void)didChangeCollectionViewDelegate:(id)delegate; +- (void)didChangeCollectionViewDelegate:(id)delegate; @end diff --git a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m index f6221489cf..f98afbb97a 100644 --- a/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m +++ b/AsyncDisplayKit/Details/ASCollectionViewFlowLayoutInspector.m @@ -36,7 +36,7 @@ return self; } -- (void)didChangeCollectionViewDelegate:(id)delegate; +- (void)didChangeCollectionViewDelegate:(id)delegate; { if (delegate == nil) { _delegateImplementsReferenceSizeForHeader = NO; diff --git a/AsyncDisplayKitTests/ASTableViewTests.m b/AsyncDisplayKitTests/ASTableViewTests.m index b4e137bcd7..a382b6be8c 100644 --- a/AsyncDisplayKitTests/ASTableViewTests.m +++ b/AsyncDisplayKitTests/ASTableViewTests.m @@ -52,7 +52,6 @@ if (_willDeallocBlock) { _willDeallocBlock(self); } - [super dealloc]; } @end @@ -78,7 +77,6 @@ if (_willDeallocBlock) { _willDeallocBlock(self); } - [super dealloc]; } @end @@ -130,6 +128,7 @@ @implementation ASTableViewTests +// TODO: Convert this to ARC. - (void)DISABLED_testTableViewDoesNotRetainItselfAndDelegate { ASTestTableView *tableView = [[ASTestTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; @@ -148,11 +147,11 @@ tableView.asyncDataSource = delegate; tableView.asyncDelegate = delegate; - - [delegate release]; + +// [delegate release]; XCTAssertTrue(delegateDidDealloc, @"unexpected delegate lifetime:%@", delegate); - XCTAssertNoThrow([tableView release], @"unexpected exception when deallocating table view:%@", tableView); +// XCTAssertNoThrow([tableView release], @"unexpected exception when deallocating table view:%@", tableView); XCTAssertTrue(tableViewDidDealloc, @"unexpected table view lifetime:%@", tableView); } @@ -399,7 +398,10 @@ style:UITableViewStylePlain asyncDataFetching:YES]; ASTableViewFilledDataSource *dataSource = [ASTableViewFilledDataSource new]; - +#if ! __has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + tableView.asyncDelegate = dataSource; tableView.asyncDataSource = dataSource;