8

CollectionViewController があります

- (void)viewDidLoad 
{
   [super viewDidLoad];    
   // assign layout (subclassed below)
   self.collectionView.collectionViewLayout = [[CustomCollectionLayout alloc] init];
}

// data source is working, here's what matters:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   ThumbCell *cell = (ThumbCell *)[cv dequeueReusableCellWithReuseIdentifier:@"ThumbCell" forIndexPath:indexPath];

   return cell;
}

UICollectionViewLayout サブクラスもあります: CustomCollectionLayout.m

#pragma mark - Overriden methods
- (CGSize)collectionViewContentSize
{
    return CGSizeMake(320, 480);
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return [[super layoutAttributesForElementsInRect:rect] mutableCopy];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // configure CellAttributes
    cellAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    // random position
    int xRand = arc4random() % 320;
    int yRand = arc4random() % 460;
    cellAttributes.frame = CGRectMake(xRand, yRand, 150, 170);

    return cellAttributes;
}

セルに新しいフレームを使用しようとしていますが、ランダムな位置に 1 つだけです。問題は、layoutAttributesForItemAtIndexPath が呼び出されないことです。

アドバイスをよろしくお願いします。

4

2 に答える 2

12

編集:あなたがあなたのケースを解決したことに気づきませんでしたが、私は同じ問題を抱えていました。ここに残しておきます。このトピックについてはまだあまり取り上げられていないため、誰かにとって役立つかもしれません。

描画時UICollectionViewにメソッドを呼び出すのではなく、layoutAttributesForItemAtIndexPath:どのlayoutAttributesForElementsInRect:セルを表示するかを決定します。このメソッドを実装し、そこからlayoutAttributesForItemAtIndexPath:すべての可視インデックス パスを呼び出して正確な場所を特定するのはユーザーの責任です。

つまり、これをレイアウトに追加します。

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray * array = [NSMutableArray arrayWithCapacity:16];
    // Determine visible index paths
    for(???) {
        [array addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:??? inSection:???]]];
    }
    return [array copy];
}
于 2012-10-24T20:59:40.193 に答える
0

ストーリーボード パラメーターに問題がありました。Inspector パネルから割り当てられたカスタム UICollectionLayout。ここに画像の説明を入力

于 2012-10-24T14:32:22.540 に答える