我有一個UITableViewCell的問題,它從xib加載並放置到表中。我用一個自定義高度和我的東西設計了一個xib單元。我有我自己的表:Swift 3 - 從xib加載的UITableViewCell - 自動佈局和高度不正確
class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate{
let sections: [String] = ["Section 1", "Section 2", "Section 3"]
let s1Data: [String] = ["Row 1", "Row 2", "Row 3"]
let s2Data: [String] = ["Row 1", "Row 2", "Row 3"]
let s3Data: [String] = ["Row 1", "Row 2", "Row 3"]
var sectionData: [Int: [String]] = [:]
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
dataSource = self
register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
register(UINib(nibName: "MySection", bundle: nil), forHeaderFooterViewReuseIdentifier: "MySection")
sectionData = [0:s1Data, 1:s2Data, 2:s3Data]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
-> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
return sections[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let section = self.dequeueReusableHeaderFooterView(withIdentifier: "MySection")
return section
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "MyCell")
return cell!
}
}
正如你所看到的一切是非常基本的,我不清理了,但(只是展示)。該部分工作正常,並顯示正確。但MyCell完全錯誤。沒有高度應用於行本身(所以內容或多或少被切割),我在xib中設置的自動佈局不存在。這只是一團糟。
我也試過以下兩行:
rowHeight = UITableViewAutomaticDimension
estimatedRowHeight = 96.0
在XIB我設置正確的限制,以迫使細胞擴大。
tableview本身只是放置在ViewController中,並配置爲MyTableView類。
那麼爲什麼高度和自動佈局不適用於單元?有人可以在這裏給我一個建議。
編輯:
這是電池的XIB文件:
這是表的結果:
,你可以看到自動佈局的東西現在起作用。也許在我身邊有一個錯誤,但身高仍然不正確
如果你想靜態的高度,而不是讓自動佈局算一下,設置'的rowHeight = 96' – redent84