要解决表格视图单元格自动高度尺寸不正常工作的问题,可以按照以下步骤进行操作:
tableView(_:cellForRowAt:)
中,确保为单元格设置合适的约束和布局。确保单元格内部的子视图可以自动调整其大小。override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
// 设置单元格的内容
cell.titleLabel.text = "标题"
cell.descriptionLabel.text = "描述"
// 设置子视图的约束和布局
cell.titleLabel.numberOfLines = 0
cell.descriptionLabel.numberOfLines = 0
return cell
}
tableView(_:heightForRowAt:)
中,返回自动计算的单元格高度。override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
viewDidLoad
方法中,设置表格视图的估算行高为一个合理的值。override func viewDidLoad() {
super.viewDidLoad()
// 设置估算行高
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableView.automaticDimension
}
通过以上步骤,表格视图的单元格应该能够根据内容自动调整高度。确保单元格中的子视图具有正确的约束和布局,并返回自动计算的高度。