gpt4 book ai didi

ios - UICollectionViewCell 中的动态高度 noScrollable UICollectionView

转载 作者:行者123 更新时间:2023-11-30 12:50:42 24 4
gpt4 key购买 nike

我想将动态高度 nonScrollable UICollectionView添加到UICollectionViewCell中。 (按照 Ash Furrow 的解释,在 UICollectionViewCell 中添加静态高度 UICollectionView 很容易)

花了几个小时在网上搜索后,我仍然不明白。

这就是我想要的

enter image description here

我们打电话吧:主要CollectionView:ParentCollectionView
CollectionView 内部:ChildCollectionView

我想获取 ChildCollectionView 的高度,以便我可以在侧面设置 ParentCollectionView 的单元格大小:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = "I'm trying to get a label in a cell.."
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let estimatedFrameForEventText = (text as NSString).boundingRect(with: CGSize.init(width: view.frame.width - 30, height: 1000), options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12)], context: nil)
return CGSize.init(width: view.frame.width - 10, height: 320 + estimatedFrameForEventText.height + 60)
}

还想用以下方法更改 ChildCollectionView 的高度常量:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: eventCellId, for: indexPath) as! EventCell
cell.eventText.text = "I'm trying to get a label in a cell.."

// height constraint of ChildCollectionView in ParentCell
cell.collectionViewHeightConstraint.constant = cell.collectionView.intrinsicContentSize.height
return cell
}

故意禁用 ChildCollectionView 的滚动,以将 ChildCollectionViewCell 全部显示在一起。

这就是我被困的地方

enter image description here

我无法预先计算 ChildCollectionView 以将它们传递给 ParentCollectionView 委托(delegate)

两个 CollectionView 都使用 UICollectionViewFlowLayout

我是 iOS 开发新手。任何帮助将不胜感激:)

最佳答案

这解决了我的问题

ParentCollectionViewController

var height: CGFloat!

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: eventCellId, for: indexPath) as! EventCell

cell.collectionViewHeightConstraint.constant = height!
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
var sizeOfChildCell: CGRect!
var widthOfCells: CGFloat! = 0.0
let cellPadding = 10
let widthOfChildCollectionview = view.frame.width - 120
for tag in tags {
sizeOfChildCell = ("#\(tag)" as NSString).boundingRect(with: CGSize.init(width: widthOfChildCollectionview, height: 1000), options: options, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12)], context: nil)
widthOfCells = widthOfCells + sizeOfChildCell.width + cellPadding + 3.8
}
let numOfRows = ceil(widthOfCells / widthOfChildCollectionview)
height = numOfRows * 25
height = height + numOfRows * 3.8
return CGSize.init(width: view.frame.width - 10, height: 320 + height)
}

ParentCollectionViewCell

var childCollectionViewHeightConstraint: NSLayoutConstraint!

lazy var childCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsVerticalScrollIndicator = false
cv.isScrollEnabled = false
cv.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
cv.backgroundColor = .white
cv.dataSource = self
cv.delegate = self
return cv
}()

override init(frame: CGRect) {
super.init(frame: frame)

childCollectionViewHeightConstraint = NSLayoutConstraint(item: childCollectionView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 96)
addConstraint(childCollectionViewHeightConstraint)

}

关于ios - UICollectionViewCell 中的动态高度 noScrollable UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41013213/

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