2

UICollectionView を使用してカスタム タイル レイアウトを作成しようとしています。アプリを実行すると、シミュレーターで必要に応じて完全にレンダリングされます。

しかし、ビューをスクロールして元に戻すと、すべてのセルのフレームが変更され、セルが重なり合ってランダムにスペースが残ります。

過去 2 日間、この問題を解決できません。カスタム レイアウト クラスのコードを次に示します。

-(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 セルでこれを行うと、スクロールすると正しくレンダリングされません。コレクション ビューをリロードしても、セルが歪んでしまいます。助けてください。

4

2 に答える 2

3

メソッドは、オブジェクトを返す前にオブジェクトlayoutAttributesForItemAtIndexPath:のプロパティを設定していません。(またはと)layoutAttributesを設定する必要があります。framecentersize

于 2013-06-03T14:21:14.390 に答える
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 内のこれらの 2 行はうまくいきましたが、コレクション ビューに約 1000 個のセルがあるため、アプリケーションのサイズが大幅に増加します。アップルがこのバグをできるだけ早く修正することを望みましょう。

于 2013-06-05T09:42:38.747 に答える