ios6 - UICollectionView:UICollectionViewCell 对象在 sizeForItemAtIndexPath 中始终为空

标签 ios6 uicollectionview uicollectionviewcell

我有一个包含不同自定义单元格的 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/

相关文章:

ios - 在退出调用方法之前显示不会更新

ios - UITableView 的第一部分被 viewForHeaderInSection 隐藏

ios - 生产模式推送通知

ios - 在 xCode 5 项目中使用 iOS6 If 语句时生成错误

ios - collectionview 只填充 iPhone 4-5 上的一个单元格

ios - 重用单元格和滚动的 UICollectionView 问题

swift - 具有 3 列的 UICollectionView 不会产生所需的结果

ios7 - UICollectionView _createPreparedCellForItem 断言失败

arrays - 基于输入的动态 CollectionView

ios - UIcollectionView scrollToitemAtIndexPath 不适用于不可见的单元格