UICollectionView委托方法cellForItemAtIndexPath:未从sizeForItemAtIndexPath调用indexPath

Pet*_*isu 4 cocoa-touch objective-c ios ios6 uicollectionview

我打电话的时候

[collectionView cellForItemAtIndexPath:indexPath:]
Run Code Online (Sandbox Code Playgroud)

从内部

[collectionView:layout:sizeForItemAtIndexPath:]
Run Code Online (Sandbox Code Playgroud)

然后不会触发委托方法.知道为什么不呢?

你可以在这里看到它.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

    [cell configureWithData:self.data[indexPath.row]];

    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];

    return [cell preferredSize];
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是询问电池的首选尺寸.

我可以

CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

但随后会触发一个永无止境的循环周期

为什么它的委托方法不应该被调用呢?

Pet*_*isu 7

我最终得到了一个Class方法而不是实例方法:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    return [CustomCell preferredSizeWithData:self.data[indexPath.row]; 

}
Run Code Online (Sandbox Code Playgroud)

我为单元格创建了一个Class方法...对于这个方法,我提供了指定的实际实例indexPath将保存并计算首选大小的数据

我觉得

CustomCell * cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

触发内部

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
Run Code Online (Sandbox Code Playgroud)

而我们所看到的是Apple的一些机制来阻止循环循环...因为直接调用

CustomCell * cell = (CustomCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

导致循环周期.