我有一个包含不同自定义单元格的 Collection View 。
这些单元格包含大小不同的不同内容。
单元格在 Storyboard 中定义,因此无需在代码中注册。
我现在要做的就是根据内容更改委托(delegate)方法中的大小:
- (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 或其内容 View 中的单元格对象。
有什么建议为什么我无法访问单元格对象?
最佳答案
collectionView:layout:sizeForItemAtIndexPath
方法由 Collection View 的流布局对象调用。布局一次要求一个单元格的大小 之前 单元格被添加到 Collection View 中。您必须“自行”计算所需的单元格大小并将其返回。
关于ios6 - UICollectionView:UICollectionViewCell 对象在 sizeForItemAtIndexPath 中始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231405/