根据标签文本动态改变单元格高度
Dynamically change cell height according to label text
我尝试为我的应用程序添加问答部分,但该 tableview 单元格必须根据文本高度展开,单元格高度必须随此文本更改。在这一点上,我做了一些事情来解决 numberOfline = 0 和 UITableView.automaticDimension 代码无法解决我的问题的问题。这是我的项目代码:
问题是单元格高度没有改变,这段代码只是在问题下写标签,在下一个问题下写更长的答案。意味着单元格高度没有改变。
let soruLabel: UILabel = {
let sorulabel = UILabel()
sorulabel.textColor = .darkGray
sorulabel.numberOfLines = 0
sorulabel.font = UIFont.boldSystemFont(ofSize: 17)
return sorulabel
}()
let cevapLabel: UILabel = {
let cevaplabel = UILabel()
cevaplabel.textColor = .black
cevaplabel.numberOfLines = 0
cevaplabel.font = UIFont(name: "HelveticaNeue", size: 18)
return cevaplabel
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func set(content: expandingTableCellForsss) {
self.soruLabel.text = content.soru
self.cevapLabel.text = content.expanded ? content.cevap : ""
}
func setup() {
backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)
addSubview(soruLabel)
addSubview(cevapLabel)
soruLabel.translatesAutoresizingMaskIntoConstraints = false
soruLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
soruLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
soruLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = false
soruLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
cevapLabel.translatesAutoresizingMaskIntoConstraints = false
cevapLabel.topAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = true
cevapLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
cevapLabel.bottomAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = false
cevapLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
请检查:
,
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
要使用动态单元格高度:
1) 在 viewDidLoad
中给出单元格的最小行高
tableView.estimatedRowHeight = 300
2) 在 viewDidLoad
中将 tableview 的 rowHeight 属性 设置为自动
tableView.rowHeight = UITableViewAutomaticDimension
3) 在故事板中 select numberOfLines = 0
4) 如果您在故事板中为单元格提供了自定义高度,请确保其等于估计的行高。
需要设置底部锚点:
cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
我尝试为我的应用程序添加问答部分,但该 tableview 单元格必须根据文本高度展开,单元格高度必须随此文本更改。在这一点上,我做了一些事情来解决 numberOfline = 0 和 UITableView.automaticDimension 代码无法解决我的问题的问题。这是我的项目代码:
问题是单元格高度没有改变,这段代码只是在问题下写标签,在下一个问题下写更长的答案。意味着单元格高度没有改变。
let soruLabel: UILabel = {
let sorulabel = UILabel()
sorulabel.textColor = .darkGray
sorulabel.numberOfLines = 0
sorulabel.font = UIFont.boldSystemFont(ofSize: 17)
return sorulabel
}()
let cevapLabel: UILabel = {
let cevaplabel = UILabel()
cevaplabel.textColor = .black
cevaplabel.numberOfLines = 0
cevaplabel.font = UIFont(name: "HelveticaNeue", size: 18)
return cevaplabel
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func set(content: expandingTableCellForsss) {
self.soruLabel.text = content.soru
self.cevapLabel.text = content.expanded ? content.cevap : ""
}
func setup() {
backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)
addSubview(soruLabel)
addSubview(cevapLabel)
soruLabel.translatesAutoresizingMaskIntoConstraints = false
soruLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
soruLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
soruLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = false
soruLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
cevapLabel.translatesAutoresizingMaskIntoConstraints = false
cevapLabel.topAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = true
cevapLabel.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
cevapLabel.bottomAnchor.constraint(equalTo: soruLabel.bottomAnchor).isActive = false
cevapLabel.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
请检查:
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
要使用动态单元格高度: 1) 在 viewDidLoad
中给出单元格的最小行高tableView.estimatedRowHeight = 300
2) 在 viewDidLoad
中将 tableview 的 rowHeight 属性 设置为自动tableView.rowHeight = UITableViewAutomaticDimension
3) 在故事板中 select numberOfLines = 0
4) 如果您在故事板中为单元格提供了自定义高度,请确保其等于估计的行高。
需要设置底部锚点:
cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true