7

我有一个包含不同自定义单元格的集合视图。

这些单元格包含大小不同的不同内容。

单元格在 Storyboard 中定义,因此无需在代码中注册。

我现在要做的就是根据内容更改委托方法中的大小:

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

id cellAtIndexPath = [collectionView cellForItemAtIndexPath:indexPath];

NSLog(@"The cell: %@", cellAtIndexPath);

...
e.g. calling sizeToFit methods and cumulating the sizes of the cells' subviews
...

}

该方法按预期为每个单元格调用,但 cellAtIndexPath 始终返回 NULL,无论我尝试什么。所以我无法访问 indexPath 或其内容视图中的单元格对象。

有什么建议为什么我无法访问单元格对象?

4

1 回答 1

11

collectionView:layout:sizeForItemAtIndexPath方法由集合视图的流布局对象调用。布局在将单元格添加到集合视图之前一次询问单元格的大小。您必须“自行”计算所需的单元格大小并将其返回。

于 2012-11-05T20:41:07.867 回答