gpt4 book ai didi

ios - 如何使 UICollectionview 单元格高度动态?

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:31 26 4
gpt4 key购买 nike

我有一个 UIcollectionview。 Collectionview 有一些单元格。在 CollectionviewCell 里面有一个 TextView 。 TextView 的内容是动态的。所以单元格高度应该是动态的。

怎么做?

最佳答案

1-添加UICollectionViewDelegateFlowLayout 协议(protocol)。
2-通过在您的 ViewController 中实现 sizeForItemAtIndexPath 方法来遵守协议(protocol)。
3-使用单元格的索引获取您拥有的文本并计算它的高度和宽度。

class ViewController: UIViewController,UICollectionViewDelegateFlowLayout{

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let constraintRect = CGSize(width: self.view.frame.size.width, height: CGFloat.max)
let data = myTextArray[indexpath.row];
let boundingBox = data.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: Constants.strings.fontName, size: 30)!], context: nil)
return CGSizeMake(boundingBox.width, boundingBox.height); //(width,hight)
}

}

关于ios - 如何使 UICollectionview 单元格高度动态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38759138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com