我有tableview單元格,里面有collectionview,還有collectionview單元格,我如何根據collectionview單元格設定tableview單元格的高度?
uj5u.com熱心網友回復:
嘗試將其添加到您的控制器中
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
uj5u.com熱心網友回復:
你的問題很模糊。
您假設提供一個您想要實作的目標的示例。
假設您的集合視圖單元格具有靜態高度:簡單,自動布局將成為您的朋友。使用:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326562.html
