Ios 更改UICollectionViewCell高度以适应UILabel

Ios 更改UICollectionViewCell高度以适应UILabel,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在努力使我的手机高度大小与标签相符。问题是标签文本是在cellForItemAtIndexPath中设置的,如果我理解正确,SizeForItemIndexPath在cellForItemAtIndexPath之前运行。这是我迄今为止尝试过的: func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCe

我正在努力使我的手机高度大小与标签相符。问题是标签文本是在
cellForItemAtIndexPath
中设置的,如果我理解正确,
SizeForItemIndexPath
cellForItemAtIndexPath
之前运行。这是我迄今为止尝试过的:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let imageCell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as? CollectionViewCell

        imageCell!.imageText.text = post.text // This set the UICollectionView Cell text

        imageCell!.frame.size.height = (imageCell!.frame.size.height) + (imageCell!.imageText.frame.size.height)

        return imageCell!
    }
我没有使用自动布局


任何关于单元格高度不随标签变化的建议?

即使先运行
SizeForestimationIndeXPath
,这也是设置单元格大小的地方
cellForItemAtIndexPath
无法更改大小

sizeForItemAtIndexPath
中,需要找出每个单元格的图像大小,然后返回该大小

大概是这样的:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let imageCell = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
    let size = CGSize(width: imageCell.frame.width, height: imageCell.frame.size.height + imageCell.imageText.frame.size.height)
    return size

}

这曾经是一个烦人的问题要解决,但如果你能使用自动布局,它会变得非常容易。有关详细的演练,请参阅。

问题我没有使用自动布局:问题是我应该如何做?我已更新,但实际上,它与您的代码相同,只是移动到了另一个函数。我收到错误:-您是否需要添加
imageCell!。frame.size.height=
还有?您发布的错误消息告诉您正确答案。您只需转换为CGSize。UpdatedBah,也会崩溃: