如何在 iOS 中更改内部表格视图单元格的单元格高度? [英] 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
}
我已经尝试设置 UITableView.automaticDiemension
,但它没有调整到文本视图.它越来越大.如果我尝试 tableView.dequeueReusableCell(withIdentifier: "profileCell") as!ProfileCell
我只得到两个单元格的内容,其余的没有得到文本值.
I have tried setting UITableView.automaticDiemension
, but it is not adjusting to the text view. It is getting larger. If I try tableView.dequeueReusableCell(withIdentifier: "profileCell") as! ProfileCell
I am getting only the contents of two cells and rest are not getting the text value.
当前的界面截图是:用户界面截图
我在 viewDidLoad()
中设置了以下表格视图配置:
I have set the following table view config in 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
再次使用单元格获取数据.然后计算高度.
When you called the func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
,you must set cellData
again but get data with cell. Then calculate the height.
这篇关于如何在 iOS 中更改内部表格视图单元格的单元格高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!