[英]How to change the cell height of inner table view cell in iOS?
我在靜態表視圖中有一個動態表視圖。 我無法根據單元格內容更改單元格高度。 我嘗試了各種方法,例如
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (ProfileData.profileViewType == .favourite) {
if let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as? ProfileCell { // gives EXC_BAD_ACCESS in SimpleCheckbox library
cell.favouritesTitleLabel.numberOfLines = 0
cell.favouritesTitleLabel.sizeToFit()
print(cell.favouritesTitleLabel.text)
let height = cell.favouritesTitleLabel.text!.height(withConstrainedWidth: cell.frame.width - 64, font: UIFont(name: "Roboto", size: 17.0)!)
print(height + 16)
return height + 16
}
}
return UITableView.automaticDimension
}
我試過設置UITableView.automaticDiemension
,但它沒有調整到文本視圖。 它越來越大。 如果我嘗試tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
我只得到兩個單元格的內容,其余的沒有得到文本值。
當前UI截圖為: UI截圖
我在viewDidLoad()
設置了以下表格視圖配置:
profileTableView = ProfileSectionTableView()
profileTableView.profileDelegate = self
profileSectionTableView.delegate = profileTableView
profileSectionTableView.dataSource = profileTableView
profileSectionTableView.rowHeight = UITableView.automaticDimension
profileSectionTableView.estimatedRowHeight = 44
當您調用func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
,您必須再次設置cellData
但使用單元格獲取數據。 然后計算高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.