From c62dc6cc038d34585ac38f45df835a1702be30b8 Mon Sep 17 00:00:00 2001 From: jiangteng Date: Fri, 13 Dec 2019 18:09:18 +0800 Subject: [PATCH] feat:implement iOS flow layout load more --- Pod/Classes/Shader/DoricFlowLayoutNode.m | 50 ++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/Pod/Classes/Shader/DoricFlowLayoutNode.m b/Pod/Classes/Shader/DoricFlowLayoutNode.m index fec2a18e..4154d78a 100644 --- a/Pod/Classes/Shader/DoricFlowLayoutNode.m +++ b/Pod/Classes/Shader/DoricFlowLayoutNode.m @@ -25,6 +25,8 @@ @protocol DoricFlowLayoutDelegate - (CGFloat)doricFlowLayoutItemHeightAtIndexPath:(NSIndexPath *)indexPath; +- (CGFloat)doricFlowLayoutItemWidthAtIndexPath:(NSIndexPath *)indexPath; +- (BOOL)isLoadView:(NSIndexPath *)indexPath; - (CGFloat)doricFlowLayoutColumnSpace; - (CGFloat)doricFlowLayoutRowSpace; @@ -93,15 +95,23 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde } } - CGFloat width = (self.collectionView.width - self.columnSpace * (self.columnCount - 1)) / self.columnCount; + CGFloat width = [self.delegate doricFlowLayoutItemWidthAtIndexPath:indexPath]; CGFloat height = [self.delegate doricFlowLayoutItemHeightAtIndexPath:indexPath]; CGFloat x = (width + self.columnSpace) * [minYOfColumn integerValue]; CGFloat y = [self.columnHeightInfo[minYOfColumn] floatValue]; if (y > 0) { y += self.rowSpace; } - self.columnHeightInfo[minYOfColumn] = @(y + height); + if (width == self.collectionView.width) { + CGFloat maxY = 0; + for (NSNumber *column in self.columnHeightInfo.allValues) { + maxY = MAX(maxY, [column floatValue]); + } + y = maxY + self.rowSpace; + }else { + self.columnHeightInfo[minYOfColumn] = @(y + height); + } UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.frame = CGRectMake(x, y, width, height); return attrs; @@ -157,6 +167,10 @@ @interface DoricFlowLayoutNode () = self.itemCount) { + return [self subModelOf:self.loadMoreViewId]; + } NSString *viewId = self.itemViewIds[@(position)]; if (viewId && viewId.length > 0) { return [self subModelOf:viewId]; @@ -281,13 +305,14 @@ - (void)callItem:(NSUInteger)position size:(CGSize)size { } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { - return self.itemCount; + return self.itemCount + (self.loadMore ? 1 : 0); } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSUInteger position = (NSUInteger) indexPath.row; NSDictionary *model = [self itemModelAt:position]; NSDictionary *props = model[@"props"]; + DoricFlowLayoutViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"doricCell" forIndexPath:indexPath]; if (!cell.viewNode) { DoricFlowLayoutItemNode *itemNode = [[DoricFlowLayoutItemNode alloc] initWithContext:self.doricContext]; @@ -295,11 +320,17 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection cell.viewNode = itemNode; [cell.contentView addSubview:itemNode.view]; } + DoricFlowLayoutItemNode *node = cell.viewNode; node.viewId = model[@"id"]; [node blend:props]; CGFloat width = (collectionView.width - (self.columnCount - 1) * self.columnSpace) / self.columnCount; CGSize size = [node.view measureSize:CGSizeMake(width, collectionView.height)]; + if (position > 0 && position >= self.itemCount && self.onLoadMoreFuncId) { + size = CGSizeMake(collectionView.width, size.height); + [self callJSResponse:self.onLoadMoreFuncId, nil]; + } + [node.view layoutSelf:size]; [self callItem:position size:size]; return cell; @@ -315,6 +346,17 @@ - (CGFloat)doricFlowLayoutItemHeightAtIndexPath:(NSIndexPath *)indexPath { } } +- (CGFloat)doricFlowLayoutItemWidthAtIndexPath:(NSIndexPath *)indexPath { + NSUInteger position = (NSUInteger) indexPath.row; + NSValue *value = self.itemSizeInfo[@(position)]; + if (value) { + return [value CGSizeValue].width; + } else { + return 100; + } +} + + - (CGFloat)doricFlowLayoutColumnSpace { return self.columnSpace; }