【发布时间】:2017-09-08 12:02:02
【问题描述】:
有很多问题回答 UITableView 的 UITableViewCell 的动态高度。但是我无法解决我的问题。
我有这个表格单元格创建代码:
class UINoteTabelViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
func fill(_ note: Note) {
let view = UINote(withNote: note, atPoint: .zero)
self.contentView.addSubview(view)
}
}
这就是我为表格视图创建单元格的方式:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotePreview", for: indexPath as IndexPath) as! UINoteTabelViewCell
cell.fill(notes[indexPath.row] as! Note)
return cell
}
表视图estimatedRowHeight 和rowHeight 也设置为UITableViewAutomaticDimension
但表格视图仍以 44.0 行高绘制。
我不知道如何解决它。
PS 我无法设置固定的estimatedRowHeight,因为每个单元格都有动态高度
【问题讨论】:
-
estimatedRowHeight不应为UITableViewAutomaticDimension。尝试将其设置为100并将rowHeight设置为 UITableViewAutomaticDimension。 -
UITableViewAutomaticDimension 与约束一起使用,您需要添加适当的约束,单元格可以通过该约束来计算其高度
-
确保您的标签编号为零行
-
尝试将
tableView.rowHeight = UITableViewAutomaticDimension写入cellForRowAtIndexPath。
标签: ios swift uitableview cell