Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 7c9698a

Browse files
committed
Merge pull request #812 from nguyenhuy/ImproveCellNodeLayoutDelegate
Improve cell node layout delegate
2 parents b7c5471 + a6807ef commit 7c9698a

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

AsyncDisplayKit/ASCellNode.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ typedef NSUInteger ASCellNodeAnimation;
1919
* The notification is done on main thread.
2020
*
2121
* @param node A node informing the delegate about the relayout.
22-
*
23-
* @param suggestedAnimation A constant indicates how the delegate should animate. See UITableViewRowAnimation.
2422
*/
25-
- (void)node:(ASCellNode *)node didRelayoutWithSuggestedAnimation:(ASCellNodeAnimation)animation;
23+
- (void)nodeDidRelayout:(ASCellNode *)node;
2624
@end
2725

2826
/**
@@ -74,13 +72,6 @@ typedef NSUInteger ASCellNodeAnimation;
7472
*/
7573
@property (nonatomic, weak) id<ASCellNodeLayoutDelegate> layoutDelegate;
7674

77-
/*
78-
* A constant that is passed to the delegate to indicate how a relayout is to be animated.
79-
*
80-
* @see UITableViewRowAnimation
81-
*/
82-
@property (nonatomic, assign) ASCellNodeAnimation relayoutAnimation;
83-
8475
/*
8576
* ASCellNode must forward touch events in order for UITableView and UICollectionView tap handling to work. Overriding
8677
* these methods (e.g. for highlighting) requires the super method be called.
@@ -91,7 +82,7 @@ typedef NSUInteger ASCellNodeAnimation;
9182
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
9283

9384
/**
94-
* Marks the node as needing layout. Convenience for use whether the view / layer is loaded or not. Safe to call from a background thread.
85+
* Marks the node as needing layout. Convenience for use whether the view / layer is loaded or not.
9586
*
9687
* If this node was measured, calling this method triggers an internal relayout: the calculated layout is invalidated,
9788
* and the supernode is notified or (if this node is the root one) a full measurement pass is executed using the old constrained size.

AsyncDisplayKit/ASCellNode.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ - (instancetype)init
2828
// use UITableViewCell defaults
2929
_selectionStyle = UITableViewCellSelectionStyleDefault;
3030
self.clipsToBounds = YES;
31-
_relayoutAnimation = UITableViewRowAnimationAutomatic;
3231

3332
return self;
3433
}
@@ -58,7 +57,7 @@ - (void)setNeedsLayout
5857

5958
if (_layoutDelegate != nil) {
6059
ASPerformBlockOnMainThread(^{
61-
[_layoutDelegate node:self didRelayoutWithSuggestedAnimation:_relayoutAnimation];
60+
[_layoutDelegate nodeDidRelayout:self];
6261
});
6362
}
6463
}

AsyncDisplayKit/ASCollectionView.mm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,9 @@ - (ASCellNode *)dataController:(ASDataController *)dataController nodeAtIndexPat
655655
{
656656
ASCellNode *node = [_asyncDataSource collectionView:self nodeForItemAtIndexPath:indexPath];
657657
ASDisplayNodeAssert([node isKindOfClass:ASCellNode.class], @"invalid node class, expected ASCellNode");
658-
node.layoutDelegate = self;
658+
if (node.layoutDelegate == nil) {
659+
node.layoutDelegate = self;
660+
}
659661
return node;
660662
}
661663

@@ -889,13 +891,11 @@ - (void)rangeController:(ASRangeController *)rangeController didDeleteSectionsAt
889891

890892
#pragma mark - ASCellNodeDelegate
891893

892-
- (void)node:(ASCellNode *)node didRelayoutWithSuggestedAnimation:(ASCellNodeAnimation)animation
894+
- (void)nodeDidRelayout:(ASCellNode *)node
893895
{
894896
ASDisplayNodeAssertMainThread();
895-
NSIndexPath *indexPath = [self indexPathForNode:node];
896-
if (indexPath != nil) {
897-
[super reloadItemsAtIndexPaths:@[indexPath]];
898-
}
897+
// Cause UICollectionView to requery for the new height of this node
898+
[super performBatchUpdates:^{} completion:nil];
899899
}
900900

901901
@end

AsyncDisplayKit/ASDisplayNode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ typedef void (^ASDisplayNodeDidLoadBlock)(ASDisplayNode *node);
549549
*
550550
* If this node was measured, calling this method triggers an internal relayout: the calculated layout is invalidated,
551551
* and the supernode is notified or (if this node is the root one) a full measurement pass is executed using the old constrained size.
552+
*
553+
* Note: ASCellNode has special behavior in that calling this method will automatically notify
554+
* the containing ASTableView / ASCollectionView that the cell should be resized, if necessary.
552555
*/
553556
- (void)setNeedsLayout;
554557

AsyncDisplayKit/ASTableView.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,9 @@ - (ASCellNode *)dataController:(ASDataController *)dataController nodeAtIndexPat
820820
{
821821
ASCellNode *node = [_asyncDataSource tableView:self nodeForRowAtIndexPath:indexPath];
822822
ASDisplayNodeAssert([node isKindOfClass:ASCellNode.class], @"invalid node class, expected ASCellNode");
823-
node.layoutDelegate = self;
823+
if (node.layoutDelegate == nil) {
824+
node.layoutDelegate = self;
825+
}
824826
return node;
825827
}
826828

@@ -895,15 +897,14 @@ - (void)willLayoutSubviewsOfTableViewCell:(_ASTableViewCell *)tableViewCell
895897
}
896898
}
897899

898-
#pragma mark - ASCellNodeDelegate
900+
#pragma mark - ASCellNodeLayoutDelegate
899901

900-
- (void)node:(ASCellNode *)node didRelayoutWithSuggestedAnimation:(ASCellNodeAnimation)animation
902+
- (void)nodeDidRelayout:(ASCellNode *)node
901903
{
902904
ASDisplayNodeAssertMainThread();
903-
NSIndexPath *indexPath = [self indexPathForNode:node];
904-
if (indexPath != nil) {
905-
[super reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimation)animation];
906-
}
905+
// Cause UITableView to requery for the new height of this node
906+
[super beginUpdates];
907+
[super endUpdates];
907908
}
908909

909910
@end

examples/Kittens/Sample/KittenNode.mm

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,17 @@ - (void)toggleImageEnlargement
191191
- (void)toggleNodesSwap
192192
{
193193
_swappedTextAndImage = !_swappedTextAndImage;
194-
[self setNeedsLayout];
194+
195+
[UIView animateWithDuration:0.15 animations:^{
196+
self.alpha = 0;
197+
} completion:^(BOOL finished) {
198+
[self setNeedsLayout];
199+
[self.view layoutIfNeeded];
200+
201+
[UIView animateWithDuration:0.15 animations:^{
202+
self.alpha = 1;
203+
}];
204+
}];
195205
}
196206

197207
@end

0 commit comments

Comments
 (0)