我正在尝试使用UITableViewAutomaticDimension行高和单元格自动布局的组合来制作可扩展单元格。为了让事情变得简单,我从以下简单的代码开始,我希望它可以工作:
import UIKit
class ViewController: UITableViewController {
let cells = [Cell(), Cell(), Cell()]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
for cell in cells {
cell.tableView = tableView
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return cells[indexPath.row]
}
}
class Cell: UITableViewCell { …Run Code Online (Sandbox Code Playgroud)