我有一个包含不同自定义单元格的集合视图.
这些单元格包含不同大小的内容.
单元格在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 ... }
对于每个单元格都会调用Method,但无论我尝试了什么,cellAtIndexPath都会返回NULL.
所以我无法访问indexPath或其内容视图中的单元格对象.
有什么建议我无法访问单元格对象?
解决方法
collectionView:layout:sizeForItemAtIndexPath方法由集合视图的流布局对象调用.在将单元格添加到集合视图之前,布局一次要求单元格的大小.您必须“自己”计算所需的单元格大小并将其返回.