【发布时间】:2017-05-11 15:47:13
【问题描述】:
我有一个关于从 xib 加载并放入表中的 UITableViewCell 的问题。我在 xib 中设计了具有自定义高度和我的东西的单元格。我有自己的桌子:
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-Class。
那么为什么高度和自动布局没有应用于单元格?有人可以在这里给我一个建议。
编辑:
这是单元格的xib文件:
这是表格的结果:
正如您所见,自动布局现在可以使用了。可能是我这边出了问题,但是高度还是不正确
【问题讨论】:
-
如果你想要静态高度而不是让自动布局为你计算,设置
rowHeight = 96
标签: ios swift xcode uitableview swift3