我的問題:我正在嘗試為表格單元格設定自動高度。但是,當我使用tableView.rowHeight = UITableView.automaticDimensionor時func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension }。如圖所示,我的 tableView 高度為0 ;但是如果我將tableView的高度設定為autoLayout,我可以再次看到我的tableView。(這是不對的,因為我希望高度是動態的,按鈕和視圖應該位于表格視圖的底部。)

這是我的 ViewController 代碼:
class ViewController: UIViewController {
var dataSource = ["1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2",]
private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.delegate = self
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.bounces = false
return scrollView
}()
private lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.backgroundColor = .clear
tableView.delegate = self
tableView.dataSource = self
tableView.register(TestTabelCell.self, forCellReuseIdentifier: TestTabelCell.identifier)
tableView.rowHeight = UITableView.automaticDimension
return tableView
}()
private lazy var moreBtn: UIButton = {
let button = UIButton()
button.setTitle("Click for More", for: .normal)
button.setTitleColor(UIColor.green, for: .normal)
return button
}()
private lazy var bottomView: UIView = {
let view = UIView()
view.backgroundColor = .brown
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupUI()
}
func setupUI() {
view.addSubview(scrollView)
scrollView.addSubview(tableView)
scrollView.addSubview(moreBtn)
scrollView.addSubview(bottomView)
scrollView.snp.makeConstraints { make in
make.top.leading.trailing.equalToSuperview()
make.bottom.equalToSuperview()
}
stackView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
tableView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.leading.trailing.equalToSuperview()
}
moreBtn.snp.makeConstraints { make in
make.top.equalTo(tableView.snp.bottom)
make.leading.trailing.equalToSuperview()
make.height.equalTo(20)
}
bottomView.snp.makeConstraints { make in
make.top.equalTo(moreBtn.snp.bottom)
make.leading.trailing.equalToSuperview()
make.width.equalTo(390)
make.height.equalTo(400)
make.bottom.equalToSuperview()
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TestTabelCell.identifier, for: indexPath)
if let cell = cell as? TestTabelCell {
cell.updateUI(String(indexPath.row))
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
print("print", UITableView.automaticDimension)
return UITableView.automaticDimension
}
}
這是我的手機代碼:
class TestTabelCell: UITableViewCell {
static let identifier = "ParlorJourneyListCell"
private lazy var label: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 12)
label.textColor = .black
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupUI() {
addSubview(label)
label.snp.makeConstraints { make in
make.top.equalToSuperview().inset(8)
make.height.equalTo(20)
make.bottom.equalToSuperview()
}
}
func updateUI(_ num: String) {
label.text = "Test Num: \(num)"
}
}
uj5u.com熱心網友回復:
實際上,您計算的只是表格視圖單元格高度,而不是表格視圖高度本身。每次reloadData()被呼叫時,您都必須重新計算表視圖高度。我假設單元格的自動布局是正確的。有幾個步驟要做:
func viewDidLoad() {
...
//Added listener to your table view contentSize if changed
tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
//Then override this function to update table view height
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard keyPath == "contentSize" else { return }
guard
let newValue = change?[.newKey],
let newHeight = (newValue as? CGSize)?.height else { return }
tableView.snp.updateConstraints {
$0.height.equalTo(newHeight)
}
}
uj5u.com熱心網友回復:
您需要在單元格的 contentView 中添加單元格的子視圖。并根據 contentView 進行約束。
func setupUI() {
contentView.addSubview(label)
label.snp.makeConstraints { make in
make.top.equalToSuperview().inset(8)
make.height.equalTo(20)
make.bottom.equalToSuperview()
}
}
像這樣更改您的代碼并立即嘗試!
contentView.addSubview(label)
uj5u.com熱心網友回復:
最后,我使用 tableView 的部分來解決我的問題。
我將整個頁面視為一個tableView,并將視圖分為2個部分,即:tableView的部分(以前是tableView,現在是一個部分)和bottomView。我認為更多按鈕作為部分頁腳。
另外,我可以通過使用兒子的答案來獲取 tableView 的高度,但我選擇將我的視圖布局在一個特定的 tableView 中,這樣我就不需要計算高度。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/525298.html
