我试图对UICollectionViewFlowLayout进行子类,以便消除单元格之间的空间,因为流布局只允许您设置minimumInterItemSpacing,而不是maximumInterItemSpacing。我需要一个垂直滚动动态单元格高度并且在单元格之间没有collectionView空间的。下面是我在UICollectionViewFlowLayout子类中尝试过的内容
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
for(int i = 1; i < [answer count]; i++) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
NSInteger maximumSpacing = 0;
NSInteger origin = prevLayoutAttributes.frame.origin.y + prevLayoutAttributes.frame.size.height;
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.height < self.collectionViewContentSize.height) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.y = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}虽然这似乎消除了项目间的间隔,但它也会产生奇怪的副作用。有些细胞会长得太高,而另一些细胞则会在大小上跳跃。而且,如果我滚动得很快,有些细胞根本就不会出现。有人知道我怎样才能最好地实现这个看似简单的事情吗?
谢谢
发布于 2014-05-10 21:49:30
您需要在布局上设置一个minimumLineSpacing:
对于垂直滚动网格,此值表示连续行之间的最小间距()。对于水平滚动网格,此值表示连续列之间的最小间距。此间距不适用于页眉和第一行之间的空格或最后一行与页脚之间的空格。
您正在设置minimumInterItemSpacing,它用于同一行中的项:
对于垂直滚动网格,此值表示同一行中项之间的最小间距。对于水平滚动网格,此值表示同一列中项之间的最小间距。这个间距用于计算在一行中可以容纳多少个项目,但是在确定了项目数量之后,实际的间距可能会向上调整。
您当前的问题是,您正在将传递给布局的rect中的项目移动,这样您就可以在布局中容纳更多的项目,但是没有添加额外的项来填充该空间,因此您看到了空白。但是,您根本不需要重写它,只需设置行距属性即可。
https://stackoverflow.com/questions/23584876
复制相似问题