[英]How to calculate or auto fit height of UiTableView in Swift 3
我的TableView內容3控制器。
1)WebView 1
2)ImageView
3)Webview 2
我正在動態設置所有3個控制器的幀大小。 因此,應根據內容大小更改行高。
self.secondWebViewLable.frame = CGRect(x: 0,
y: Int(self.firstImage.frame.origin.y + self.firstImage.frame.height),
width: Int(screenSize.width),
height: Int(self.secondWebViewLable.frame.height) )
self.firstImage.frame = CGRect(x: 0,
y: Int(self.firstWebViewLable.frame.origin.y + self.firstWebViewLable.frame.height),
width: Int(screenSize.width),
height: Int(scaledHeight) )
您會看到控制器的高度正在根據數據而變化。 我也在嘗試設置單元格的高度。 下面是我的代碼。 但是我固定了高度。 請幫助我。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return screenSize.height
}
使用自動布局
在viewDidLoad()中設置估計的行高,並從heightForRowAt indexPath方法返回UITableViewAutomaticDimension 。
請參閱本教程以獲取更多詳細說明: self-sizing-table-view-cells
沒有自動布局
如果沒有自動布局,則必須通過將單元格中所有UI組件的高度和邊距相加來計算每個單元格的高度,並在UITableView數據源方法tableView:heightForRowAtIndexPath中返回計算出的高度:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.