From 3e6436245d57ff3a494d965061bbf394b9ad4bf8 Mon Sep 17 00:00:00 2001 From: Li Tan Date: Thu, 18 Dec 2014 13:04:02 -0800 Subject: [PATCH] fix inserting sections bug --- AsyncDisplayKit/Details/ASDataController.h | 4 ++-- AsyncDisplayKit/Details/ASDataController.mm | 19 +++++++++++++------ .../Details/ASFlowLayoutController.mm | 12 ++++++++++-- AsyncDisplayKit/Details/ASLayoutController.h | 2 +- AsyncDisplayKit/Details/ASRangeController.mm | 16 ++++++++++++++-- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/AsyncDisplayKit/Details/ASDataController.h b/AsyncDisplayKit/Details/ASDataController.h index 599060ae1b..c1f59561fd 100644 --- a/AsyncDisplayKit/Details/ASDataController.h +++ b/AsyncDisplayKit/Details/ASDataController.h @@ -57,8 +57,8 @@ /** Called for insertion of sections. */ -- (void)dataController:(ASDataController *)dataController willInsertSectionsAtIndexSet:(NSIndexSet *)indexSet; -- (void)dataController:(ASDataController *)dataController didInsertSectionsAtIndexSet:(NSIndexSet *)indexSet; +- (void)dataController:(ASDataController *)dataController willInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet; +- (void)dataController:(ASDataController *)dataController didInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet; /** Called for deletion of sections. diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index 45b65f9647..5f6e2ee0ac 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -33,12 +33,12 @@ #define INSERT_SECTIONS(multidimensionalArray, indexSet, sections) \ { \ - if ([_delegate respondsToSelector:@selector(dataController:willInsertSectionsAtIndexSet:)]) { \ - [_delegate dataController:self willInsertSectionsAtIndexSet:indexSet]; \ + if ([_delegate respondsToSelector:@selector(dataController:willInsertSections:atIndexSet:)]) { \ + [_delegate dataController:self willInsertSections:sections atIndexSet:indexSet]; \ } \ [multidimensionalArray insertObjects:sections atIndexes:indexSet]; \ - if ([_delegate respondsToSelector:@selector(dataController:didInsertSectionsAtIndexSet:)]) { \ - [_delegate dataController:self didInsertSectionsAtIndexSet:indexSet]; \ + if ([_delegate respondsToSelector:@selector(dataController:didInsertSections:atIndexSet:)]) { \ + [_delegate dataController:self didInsertSections:sections atIndexSet:indexSet]; \ } \ } @@ -206,8 +206,15 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext; - (void)insertSections:(NSIndexSet *)indexSet { NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:indexSet.count]; - for (int i = 0; i < indexSet.count; i++) { - [sectionArray addObject:[[NSMutableArray alloc] init]]; + for (NSUInteger i = 0; i < indexSet.count; i++) { + NSUInteger rowNum = [_dataSource dataController:self rowsInSection:i]; + NSMutableArray *rows = [[NSMutableArray alloc] initWithCapacity:rowNum]; + + for (NSUInteger j = 0; j < rowNum; j++) { + ASCellNode *node = [_dataSource dataController:self nodeAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]]; + [rows addObject:node]; + } + [sectionArray addObject:rows]; } dispatch_async([[self class] sizingQueue], ^{ diff --git a/AsyncDisplayKit/Details/ASFlowLayoutController.mm b/AsyncDisplayKit/Details/ASFlowLayoutController.mm index 7101a1103b..91f35b91d6 100644 --- a/AsyncDisplayKit/Details/ASFlowLayoutController.mm +++ b/AsyncDisplayKit/Details/ASFlowLayoutController.mm @@ -53,9 +53,17 @@ static const CGFloat kASFlowLayoutControllerRefreshingThreshold = 0.3; }]; } -- (void)insertSectionsAtIndexSet:(NSIndexSet *)indexSet { +- (void)insertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet { + __block int cnt = 0; [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { - _nodeSizes.insert(_nodeSizes.begin() + idx, std::vector()); + NSArray *nodes = sections[cnt++]; + std::vector v(nodes.count); + + for (int i = 0; i < nodes.count; i++) { + v.push_back([nodes[i] CGSizeValue]); + } + + _nodeSizes.insert(_nodeSizes.begin() + idx, v); }]; } diff --git a/AsyncDisplayKit/Details/ASLayoutController.h b/AsyncDisplayKit/Details/ASLayoutController.h index faa7f5c073..d6998bb1b8 100644 --- a/AsyncDisplayKit/Details/ASLayoutController.h +++ b/AsyncDisplayKit/Details/ASLayoutController.h @@ -28,7 +28,7 @@ typedef NS_ENUM(NSInteger, ASScrollDirection) { - (void)deleteNodesAtIndexPaths:(NSArray *)indexPaths; -- (void)insertSectionsAtIndexSet:(NSIndexSet *)indexSet; +- (void)insertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet; - (void)deleteSectionsAtIndexSet:(NSIndexSet *)indexSet; diff --git a/AsyncDisplayKit/Details/ASRangeController.mm b/AsyncDisplayKit/Details/ASRangeController.mm index d31fabfeab..c394d6fbdb 100644 --- a/AsyncDisplayKit/Details/ASRangeController.mm +++ b/AsyncDisplayKit/Details/ASRangeController.mm @@ -245,9 +245,21 @@ }]; } -- (void)dataController:(ASDataController *)dataController didInsertSectionsAtIndexSet:(NSIndexSet *)indexSet { +- (void)dataController:(ASDataController *)dataController didInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet { + ASDisplayNodeAssert(sections.count == indexSet.count, @"Invalid sections"); + + NSMutableArray *sectionNodeSizes = [NSMutableArray arrayWithCapacity:sections.count]; + + [sections enumerateObjectsUsingBlock:^(NSArray *nodes, NSUInteger idx, BOOL *stop) { + NSMutableArray *nodeSizes = [NSMutableArray arrayWithCapacity:nodes.count]; + [nodes enumerateObjectsUsingBlock:^(ASCellNode *node, NSUInteger idx, BOOL *stop) { + [nodeSizes addObject:[NSValue valueWithCGSize:node.calculatedSize]]; + }]; + [sectionNodeSizes addObject:nodeSizes]; + }]; + [self updateOnMainThreadWithBlock:^{ - [_layoutController insertSectionsAtIndexSet:indexSet]; + [_layoutController insertSections:sectionNodeSizes atIndexSet:indexSet]; [_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet]; }]; }