【发布时间】:2021-11-25 11:11:27
【问题描述】:
我有tableview单元格,里面有collectionview,还有collectionview单元格,如何根据collectionview单元格设置tableview单元格的高度?
【问题讨论】:
标签: ios swift uitableview uicollectionview
我有tableview单元格,里面有collectionview,还有collectionview单元格,如何根据collectionview单元格设置tableview单元格的高度?
【问题讨论】:
标签: ios swift uitableview uicollectionview
尝试将其添加到您的控制器中
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
【讨论】:
你的问题很模糊。
你想提供一个你想要达到的目标的例子。
假设您有集合视图单元格的静态高度: 简单,自动布局将成为您的朋友。 使用:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
但是,如果您想动态布局表格视图单元格高度:
// You may need to call it when viewDidLayoutSubviews() is called
let height = collectionView.collectionViewLayout.collectionViewContentSize.height
collectionViewHeightConstraint.constant = height
contentView.layoutIfNeeded()
【讨论】: