fix inserting sections bug

This commit is contained in:
Li Tan
2014-12-18 13:04:02 -08:00
parent 4886d3d1ad
commit 3e6436245d
5 changed files with 40 additions and 13 deletions

View File

@@ -57,8 +57,8 @@
/** /**
Called for insertion of sections. Called for insertion of sections.
*/ */
- (void)dataController:(ASDataController *)dataController willInsertSectionsAtIndexSet:(NSIndexSet *)indexSet; - (void)dataController:(ASDataController *)dataController willInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet;
- (void)dataController:(ASDataController *)dataController didInsertSectionsAtIndexSet:(NSIndexSet *)indexSet; - (void)dataController:(ASDataController *)dataController didInsertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet;
/** /**
Called for deletion of sections. Called for deletion of sections.

View File

@@ -33,12 +33,12 @@
#define INSERT_SECTIONS(multidimensionalArray, indexSet, sections) \ #define INSERT_SECTIONS(multidimensionalArray, indexSet, sections) \
{ \ { \
if ([_delegate respondsToSelector:@selector(dataController:willInsertSectionsAtIndexSet:)]) { \ if ([_delegate respondsToSelector:@selector(dataController:willInsertSections:atIndexSet:)]) { \
[_delegate dataController:self willInsertSectionsAtIndexSet:indexSet]; \ [_delegate dataController:self willInsertSections:sections atIndexSet:indexSet]; \
} \ } \
[multidimensionalArray insertObjects:sections atIndexes:indexSet]; \ [multidimensionalArray insertObjects:sections atIndexes:indexSet]; \
if ([_delegate respondsToSelector:@selector(dataController:didInsertSectionsAtIndexSet:)]) { \ if ([_delegate respondsToSelector:@selector(dataController:didInsertSections:atIndexSet:)]) { \
[_delegate dataController:self didInsertSectionsAtIndexSet:indexSet]; \ [_delegate dataController:self didInsertSections:sections atIndexSet:indexSet]; \
} \ } \
} }
@@ -206,8 +206,15 @@ static void *kASDataUpdatingQueueContext = &kASDataUpdatingQueueContext;
- (void)insertSections:(NSIndexSet *)indexSet { - (void)insertSections:(NSIndexSet *)indexSet {
NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:indexSet.count]; NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:indexSet.count];
for (int i = 0; i < indexSet.count; i++) { for (NSUInteger i = 0; i < indexSet.count; i++) {
[sectionArray addObject:[[NSMutableArray alloc] init]]; 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], ^{ dispatch_async([[self class] sizingQueue], ^{

View File

@@ -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) { [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
_nodeSizes.insert(_nodeSizes.begin() + idx, std::vector<CGSize>()); NSArray *nodes = sections[cnt++];
std::vector<CGSize> v(nodes.count);
for (int i = 0; i < nodes.count; i++) {
v.push_back([nodes[i] CGSizeValue]);
}
_nodeSizes.insert(_nodeSizes.begin() + idx, v);
}]; }];
} }

View File

@@ -28,7 +28,7 @@ typedef NS_ENUM(NSInteger, ASScrollDirection) {
- (void)deleteNodesAtIndexPaths:(NSArray *)indexPaths; - (void)deleteNodesAtIndexPaths:(NSArray *)indexPaths;
- (void)insertSectionsAtIndexSet:(NSIndexSet *)indexSet; - (void)insertSections:(NSArray *)sections atIndexSet:(NSIndexSet *)indexSet;
- (void)deleteSectionsAtIndexSet:(NSIndexSet *)indexSet; - (void)deleteSectionsAtIndexSet:(NSIndexSet *)indexSet;

View File

@@ -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:^{ [self updateOnMainThreadWithBlock:^{
[_layoutController insertSectionsAtIndexSet:indexSet]; [_layoutController insertSections:sectionNodeSizes atIndexSet:indexSet];
[_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet]; [_delegate rangeController:self didInsertSectionsAtIndexSet:indexSet];
}]; }];
} }