gpt4 book ai didi

ios - 在 iOS8+ 上使用 AutoLayout 的自定大小的 TableView 单元格内的可变高度 CollectionView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:13 26 4
gpt4 key购买 nike

我有一个 UITableView,它使用 UITableViewAutomaticDimension 和 AutoLayout 约束来调整单元格的高度。在其中一个原型(prototype)单元格上,我需要添加 UICollectionViewUICollectionViewFlowLayout,这将在紧凑尺寸类的单列或常规尺寸类的 3 列网格中垂直布置单元格。因此, Collection View 的高度是动态的,即使在 iPhone 6 Plus 上旋转时布局也可能发生变化(紧凑->常规)。

我在 Storyboard中使用自定义原型(prototype)单元设置我的 TableView ,并将自定义 Collection View 添加到原型(prototype)单元中,AutoLayout 约束始终为零到 super View 的相应边缘(前导、尾随、顶部、底部 = 0 到 super View ).. . collectionView->tableviewContentView->tableviewCell.

我需要 Collection View 的可变高度来驱动 TableView 原型(prototype)单元格的高度。到目前为止,我总是在运行时遇到以下 AutoLayout 错误。

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

你能建议正确的方法吗?谢谢!

主 TableViewController:

class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension
}
/// ... plus tableview datasource functions
}

Collection View :

class AttachmentCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {

override func awakeFromNib() {
let nib = UINib(nibName: Constants.AttachmentCollectionView.cellNibName, bundle: nil)
registerNib(nib, forCellWithReuseIdentifier: Constants.AttachmentCollectionView.cellIdentifier)
collectionViewLayout = attachmentFlowLayout()
dataSource = self
delegate = self
backgroundColor = UIColor.whiteColor()
}

private func attachmentFlowLayout() -> UICollectionViewFlowLayout {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Vertical
layout.itemSize = CGSize(width: 70, height: 50)
layout.sectionInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
layout.minimumLineSpacing = 0.0
layout.minimumInteritemSpacing = 0.0
layout.headerReferenceSize = CGSizeMake(0.0, 0.0)
layout.footerReferenceSize = CGSizeMake(0.0, 0.0)
return layout
}
// ...plus collectionview datasource functions
}

最佳答案

我想实现tableView的委托(delegate)方法tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
可能有效。

关于ios - 在 iOS8+ 上使用 AutoLayout 的自定大小的 TableView 单元格内的可变高度 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34277102/

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