我已经实施了Row&基于列CollectionView
并使用customlayout
UICollectionView
启用它,并且按预期正常工作并使用固定width
& Row& height
列。
但不幸的是,它与动态width
& Row& height
列。
下面是固定高度&的屏幕截图。宽度输出CollectionView
:
我使用下面的代码来实现上面的动态自定义布局:
- (void)prepareLayout {
[super prepareLayout];
self.gridRowCount = [self.collectionView numberOfSections];
self.gridColumnCount = [self.collectionView numberOfItemsInSection:0];
if (!self.isInitialized) {
id<UICollectionViewDelegateFlowLayout> delegate = (id)self.collectionView.delegate;
CGSize size = [delegate collectionView:self.collectionView
layout:self
sizeForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
self.itemSize = size;
self.isInitialized = YES;
}
}
- (CGSize)collectionViewContentSize {
if (!self.isInitialized) {
[self prepareLayout];
}
CGSize size = CGSizeMake(self.gridColumnCount * self.itemSize.width, self.gridRowCount * self.itemSize.height);
return size;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *attributes = [NSMutableArray array];
NSUInteger startRow = floorf(rect.origin.y / self.itemSize.height);
NSUInteger startCol = floorf(rect.origin.x / self.itemSize.width);
NSUInteger endRow = MIN(self.gridRowCount - 1, ceilf(CGRectGetMaxY(rect) / self.itemSize.height));
NSUInteger endCol = MIN(self.gridColumnCount - 1, ceilf(CGRectGetMaxX(rect) / self.itemSize.width));
NSParameterAssert(self.gridRowCount > 0);
NSParameterAssert(self.gridColumnCount > 0);
for (NSUInteger row = startRow; row <= endRow; row++) {
for (NSUInteger col = startCol; col <= endCol; col++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:col inSection:row];
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[attributes addObject:layoutAttributes];
}
}
return attributes;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(indexPath.item * self.itemSize.width, indexPath.section * self.itemSize.height, self.itemSize.width-self.cellSpacing, self.itemSize.height-self.cellSpacing);
return attributes;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return NO;
}
我想要单元格和列的动态大小,它根本不会合脚并且滚动顺畅。
注意:我要在此CollectionView
中显示超过200K的记录。它在2K行没有任何滞后的情况下工作但在此之后需要花时间来布局行和放大行。列。