2013-06-03 84 views
1

我想使用UICollectionView創建自定義平鋪佈局。 一旦我運行我的應用程序,它將在模擬器中完美呈現。集合視圖,自定義佈局,滾動單元格行爲不端

但是,當我滾動視圖並將其恢復時,所有單元格的框架會發生變化,並且單元格會發生重疊,從而留下空間,並隨機。

我無法在兩天內解決此問題。 這裏是我自定義佈局類的代碼。

-(void)prepareLayout{ 
[self createCellSizeArray];//cellSizeArray holds cell sizes for all the cells(calculated statically) 
[self createAttributeArrayOfAll];//attributeArrayOfAll holds attributes for all the cells and also calculates their frames using cellSizeArray 
} 

-(CGSize)collectionViewContentSize{ 
    return CGSizeMake(768, 1500);//The size is static to check for scrolling, is this creating problems? 
} 

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ 
     UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
     return layoutAttributes; 
} 

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ 
    NSMutableArray *attArray =[NSMutableArray array]; 
    for (NSInteger i =0; i< attributeArrayOfAll.count; i++) { 
    UICollectionViewLayoutAttributes * attribute = (UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:i]; 
    if(CGRectIntersectsRect(rect, attribute.frame)){ 
     [attArray addObject:attribute]; 
    } 
} 
return attArray; 
} 

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{ 
    return YES; 
} 

請幫忙,提前致謝。

編輯: 在我[self createAttributeArrayOfAll];我的代碼

CGRect frame = CGRectMake(_startNewRowPoint.x, _startNewRowPoint.y, cellSize.width, cellSize.height); 
UICollectionViewLayoutAttributes * attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; 
attribute.alpha = 1.0; 
attribute.frame = frame; 
[attributeArrayOfAll addObject:attribute]; 

這些線路雖然我修改layoutAttributesForItemAtIndexPath:,看起來像這樣

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ 
UICollectionViewLayoutAttributes * layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
layoutAttributes.frame = ((UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:indexPath.item]).frame; 
return layoutAttributes; 
} 

而且,該方法layoutAttributesForItemAtIndexPath:不會被調用含蓄。我甚至試過這樣:

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ 
NSMutableArray *attArray =[NSMutableArray arrayWithCapacity:attributeArrayOfAll.count]; 
for (NSInteger i =0; i< attributeArrayOfAll.count; i++) { 
    UICollectionViewLayoutAttributes * attribute = (UICollectionViewLayoutAttributes*)[attributeArrayOfAll objectAtIndex:i]; 
    if(CGRectIntersectsRect(rect, attribute.frame)){ 
     [attArray insertObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]] atIndex:i]; 
    } 
} 
return attArray; 
} 

但仍是結果是一樣的扭曲集上滾動的細胞。 我用5個單元格,第一次正確渲染,滾動,然後把它帶回可見矩形,它會變形,如果我再次滾動並將它帶回可見矩形,它會呈現正確。但是,當我用400個左右的單元格進行操作時,一旦我滾動它就無法正確渲染。即使在重新加載收藏視圖時,單元格也會變形。請幫忙。

回答

0

所以最後管理了一個解決方法!出隊有獨特的小區標識每個單元cellForRow:

[self.summaryView registerClass:[BFSSummaryViewCell class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d",CellIdentifier,indexPath.row]]; 
UICollectionViewCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d",CellIdentifier,indexPath.row] forIndexPath:indexPath]; 

內cellForRow這兩條線爲我工作,但與具有大約1000個細胞在我的收藏查看它增加了我的應用程序的大小相當。讓希望蘋果儘快修復這個錯誤。

+3

對不起,但這是解決這個問題的可怕方法。設置UICollectionView類的體系結構,以便重用單元。如果你對每一個可能的單元格都有不同的單元類,那麼你只會讓自己變得困難。嘗試解決您遇到的實際問題,而不使用此黑客攻擊。 – buildsucceeded

+0

@buildsucceeded我知道這是一個傷心和不愉快的出路。但是我發現了這個問題的一個開放式雷達,儘管嘗試了幾乎所有的東西,但沒有別的選擇。如果您可以分享更好的解決方案,那麼您非常歡迎。相信我,我的收藏視圖在滾動上變得非常暴躁。我正在做噩夢! :) –

+0

看起來你試圖對自己的細胞進行太多的管理,即使沒有這種「解決方法」(每個細胞有一個類別)。你在哪裏調用cellForItemAtIndexPath和出列?如果我是你,我會重新開始一個全新的項目,並確保在我開始覆蓋你正在使用的任何方法之前,出隊工作不會弄亂屬性。 – buildsucceeded

3

您的layoutAttributesForItemAtIndexPath:方法在返回之前未設置layoutAttributes對象的任何屬性。它需要設置frame(或centersize)。

相關問題