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

当我打电话

[collectionView cellForItemAtIndexPath:indexPath:] 

从内部

 [collectionView:layout:sizeForItemAtIndexPath:] 

那么委托方法不会被触发。 任何想法为什么不呢?

你可以在这里看到它。

 - (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 prefferedSize]; } 

我想要做的是要求单元格的大小。

我可以

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

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

为什么不调用它的代理方法?

我结束了一个类方法,而不是一个实例方法

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return [CustomCell prefferedSizeWithData:self.data[indexPath.row]; } 

我为单元格做了一个类方法…对于这个方法,我提供了指定indexPath中的一个实例实例将保存的数据,并计算出优先的大小

我那件事

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

触发内部

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

我们所看到的是来自苹果的一些机制,以防止循环…因为直接调用

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

导致循环周期