新建一个基与 UICollectionViewFlowLayout 类,在创建UICollectionVIew时,使用新建的Layout,在相关文件中添加对应文件内容; 新建一个基与 UICollectionViewFlowLayout 类,在创建UICollectionVIew时,使用新建的Layout,在相关文件中添加对应文件内容;新建一个基与 UICollectionViewFlowLayout 类,在创建UICollectionVIew时,使用新建的Layout,在相关文件中添加对应文件内容;
.h
@property (nonatomic) CGFloat maximumInteritemSpacing;
.m
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray * layoutAttributes_t = [super layoutAttributesForElementsInRect:rect]; NSArray * layoutAttributes = [[NSArray alloc]initWithArray:layoutAttributes_t copyItems:YES]; //用来临时存放一行的Cell数组 NSMutableArray * layoutAttributesTemp = [[NSMutableArray alloc]init]; for (NSUInteger index = 0; index < layoutAttributes.count ; index++) { UICollectionViewLayoutAttributes *currentAttr = layoutAttributes[index]; // 当前cell的位置信息 UICollectionViewLayoutAttributes *previousAttr = index == 0 ? nil : layoutAttributes[index-1]; // 上一个cell 的位置信 UICollectionViewLayoutAttributes *nextAttr = index + 1 == layoutAttributes.count ? nil : layoutAttributes[index+1];//下一个cell 位置信息 //加入临时数组 [layoutAttributesTemp addObject:currentAttr]; CGFloat previousY = previousAttr == nil ? 0 : CGRectGetMaxY(previousAttr.frame); CGFloat currentY = CGRectGetMaxY(currentAttr.frame); CGFloat nextY = nextAttr == nil ? 0 : CGRectGetMaxY(nextAttr.frame); //如果当前cell是单独一行 if (currentY != previousY && currentY != nextY){ if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { [layoutAttributesTemp removeAllObjects]; }else if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionFooter]){ [layoutAttributesTemp removeAllObjects]; }else{ [self setCellFrameWith:layoutAttributesTemp]; } } //如果下一个不cell在本行,则开始调整Frame位置 else if( currentY != nextY) { [self setCellFrameWith:layoutAttributesTemp]; } } return layoutAttributes; } -(void)setCellFrameWith:(NSMutableArray*)layoutAttributes{ CGFloat nowWidth = 0.0; nowWidth = self.sectionInset.left; for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) { CGRect nowFrame = attributes.frame; nowFrame.origin.x = nowWidth; attributes.frame = nowFrame; nowWidth += nowFrame.size.width + self.maximumInteritemSpacing; } [layoutAttributes removeAllObjects]; }
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!