- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我希望能够在 sizeForItemAtIndexPath
函数中调用我的 UICollectionViewCell
类。像这样:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cell = MyCollectionViewCell()
let itemHeights = cell.titleLabel.frame.size.height + cell.subtitleLabel.frame.size.height + 20
return CGSize(width: self.view.frame.size.width - 30, height: itemHeights + cell.thumbnail.frame.size.height)
}
问题在于 cell.titleLabel.frame.size.height
、cell.subtitleLabel.frame.size.height
和 cell.thumbnail.frame .size.height
都返回 nil
。我认为这是因为每当调用 sizeForItemAtIndexPath
时,单元格尚未加载并且尚未调用 cellForItemAtIndexPath
。
我需要知道这一点,因为 cell.titleLabel
可以是在 cellForItemAtIndexPath
中设置的多条线和宽度。
有什么想法吗?
最佳答案
sizeForItemAt
在您的 cell
实际创建并配置所需数据之前被调用。这就是您无法获得所需的正确数据的原因。
试试这个:
通过 dequeuing
从 collectionView
中创建一个 dummy cell
在 sizeForItemAt
中。使用要显示的实际数据配置单元格。配置后获取您需要的数据,即
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
//Configure your cell with actual data
cell.contentView.layoutIfNeeded()
//Calculate the size and return
}
关于ios - 在 sizeForItemAtIndexPath 中调用 UICollectionView Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861553/
关于模型- View - Controller (MVC) 的一个非常基本的问题。 我有一个自定义 UICollectionView,其中填充了自定义 UICollectionViewCells。 C
我已经让我的 Collection View 单元格以编程方式计算,并且我正在使用 sizeForItemAtIndexPath 方法来设置单元格的大小。 但是当屏幕旋转时,我需要重新计算尺寸并为单元
我有一个带有 UICollectionViewFlowLayout 的 UICollectionView。在使用大数据(5,000 多个单元格)进行测试时,我注意到加载时存在延迟。我发现在加载时,每个
这让我发疯!我有一个 UICollectionViewController,如下所示: class PhrasesCompactCollectionViewController: UICollecti
在过去的 5 天里,我一直在为这个问题苦苦挣扎,无法取得任何重大突破。 背景:我有一个带有自定义布局的 UICollectionView。 UICollectionView 有一个 textView
我试图在 UICollectionViewCellCell 中创建一个 UICollectionView,但是函数 sizeForItemAtIndexPath 和 cellForItemAtInde
所以很有趣,我正在尝试使用 collectionViewLayout sizeForItemAtIndexPath 在 collectionView 中一次只显示一个项目并水平滚动,我试过这个: -
我正在尝试实现 UICollectionViewDelegateFlowlayout 的 sizeForItemAtIndexPath,类似于本教程:http://www.raywenderlich.
我想为调整 UICollectionViewCell 的大小设置动画。我已经编写了下面的代码,但不能在动画 block 中包含 return 行。有什么想法吗? func collectionView
如果我有一个具有自动布局的单元格,我正在循环从哪里获取 Collection View 中单元格的大小 我知道 sizeForItemAtIndexPath 应该从数据而不是单元格本身获取大小,因为单
我正在使用 UICollectionView 但未找到以下方法: func collectionView(collectionView: UICollectionView, layout colle
我有一个包含不同自定义单元格的 Collection View 。 这些单元格包含大小不同的不同内容。 单元格在 Storyboard 中定义,因此无需在代码中注册。 我现在要做的就是根据内容更改委托
我有以下代码,它将 Collection View 放入一列或两列中,具体取决于屏幕的大小: func collectionView(collectionView: UICollectionView,
我在具有两个 Collection View 的 View Controller 中使用 sizeForItemAtIndexPath 函数。 如何在 sizeForItemAtIndexPath 函
我能够在一个非常简单的独立应用程序上重现它。 我有一个 collectionView,我想制作循环/循环,以便元素一次又一次地重复(也就是当用户位于数组中的最后一个元素时,它会再次显示第一个元素。如果
我希望能够在 sizeForItemAtIndexPath 函数中调用我的 UICollectionViewCell 类。像这样: func collectionView(_ collectionVi
我正在使用 collectionView 来显示从 Coredatabase 获取的图像。但是,除 numberOfItemsInSection 和 numberOfSectionsInCollect
当我调用以下委托(delegate)方法时: - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollect
我在我的 UICollectionView 和我的 sizeForItemAtIndexPath 函数中有可变宽度,我最终返回一个 CGSize func collectionView(_ colle
我是 iOS 开发的新手,我正在尝试将我在 Android 上的项目移植到 iOS。我正在尝试在代码中构建一个 collectionView 作为实现的一部分。事情是因为 Collection Vie
我是一名优秀的程序员,十分优秀!