[英]How to change the cell height of inner table view cell in iOS?
I have a dynamic table view inside a static table view.我在静态表视图中有一个动态表视图。 I am not able to change the cell height according to the cell content.
我无法根据单元格内容更改单元格高度。 I have tried various methods like
我尝试了各种方法,例如
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
}
I have tried setting UITableView.automaticDiemension
, but it is not adjusting to the text view.我试过设置
UITableView.automaticDiemension
,但它没有调整到文本视图。 It is getting larger.它越来越大。 If I try
tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
如果我尝试
tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
I am getting only the contents of two cells and rest are not getting the text value. tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
我只得到两个单元格的内容,其余的没有得到文本值。
The current UI screenshot is: UI screenshot当前UI截图为: UI截图
I have set the following table view config in viewDidLoad()
:我在
viewDidLoad()
设置了以下表格视图配置:
profileTableView = ProfileSectionTableView()
profileTableView.profileDelegate = self
profileSectionTableView.delegate = profileTableView
profileSectionTableView.dataSource = profileTableView
profileSectionTableView.rowHeight = UITableView.automaticDimension
profileSectionTableView.estimatedRowHeight = 44
When you called the func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
,you must set cellData
again but get data with cell.当您调用
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
,您必须再次设置cellData
但使用单元格获取数据。 Then calculate the height.然后计算高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.