我試圖繼承UICollectionViewFlowLayout
的子類,以便我可以消除單元格之間的空間,因爲流佈局只允許您設置minimumInterItemSpacing
而不是maximumInterItemSpacing
。我需要一個與動態單元格高度垂直滾動的collectionView,並在單元格之間有空間。以下是我在我的UICollectionViewFlowLayout
子曾嘗試:UICollectionView與零interItemSpacing
- (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;
}
雖然這似乎消除interitem間距,它也產生奇怪的副作用。一些細胞結束的方式太高,另一些細胞大小跳躍。另外,如果我滾動的速度非常快,一些單元格根本不會顯示出來。有誰知道我能如何最好地實現這個看似簡單的事情?
由於
您是否每行有多個單元格,還是像堆疊一樣堆疊在一起? – jrturton
這裏的問題是,即使您在該矩形中移動項目的位置,您仍然可以爲特定矩形返回相同數量的佈局屬性對象,因此您可以在其中放入額外的單元格 - 這就是爲什麼您會看到空白 – jrturton