如何自动调整tableView高度+ StackView
我正在尝试实现此功能:
在表格视图的每个单元格中显示标题、描述和图像。 然而,图像可能为零。 要求是:
- 当图像为零时,自动扩展标签以适合整个单元格
- Tableview 单元格高度应根据描述标签自动调整。
- 需要使用 stackView
我以编程方式执行此操作:
设置这是表视图:
tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 200
创建 2 个 Stackview:一个用于标签,一个用于标签 + 图像:
let contentStack: UIStackView = {
var view = UIStackView()
view.axis = .horizontal
view.spacing = 16
view.distribution = .equalSpacing
view.translatesAutoresizingMaskIntoConstraints = false
return view }()
let labelStack: UIStackView = {
var view = UIStackView()
view.axis = .vertical
view.spacing = 5
view.translatesAutoresizingMaskIntoConstraints = false
view.distribution = .fillProportionally
return view }()
- 使用自动布局设置约束:
func setupUI() {
contentView.addSubview(contentStack)
labelStack.addArrangedSubview(titleLabel)
labelStack.addArrangedSubview(descriptionLable)
contentStack.addArrangedSubview(labelStack)
contentStack.addArrangedSubview(iconView)
NSLayoutConstraint.activate([
iconView.heightAnchor.constraint(equalToConstant: 80),
iconView.widthAnchor.constraint(equalToConstant: 80),
contentStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
contentStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
contentStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
contentStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
])
layoutIfNeeded()
}
但是,此代码不会自动调整 tableview 单元格的高度,您可以从屏幕截图中看到,单元格不会基于关于标签的长度。
使用 stackview 执行此操作的最佳方法是什么?
I'm trying to implement this feature:
Show title, description and image in each cell of a tableview.
However, the image could be nil.
Requirement is:
- When image is nil, auto expand the label to fit the whole cell
- Tableview cell height should be adjusted automatically based on the description label.
- Need to use stackView
I did this programmatically:
Set this is tableview:
tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 200
Create 2 Stackviews: one for labels, one for labels + image:
let contentStack: UIStackView = {
var view = UIStackView()
view.axis = .horizontal
view.spacing = 16
view.distribution = .equalSpacing
view.translatesAutoresizingMaskIntoConstraints = false
return view }()
let labelStack: UIStackView = {
var view = UIStackView()
view.axis = .vertical
view.spacing = 5
view.translatesAutoresizingMaskIntoConstraints = false
view.distribution = .fillProportionally
return view }()
- Use autolayout to setup the constraints:
func setupUI() {
contentView.addSubview(contentStack)
labelStack.addArrangedSubview(titleLabel)
labelStack.addArrangedSubview(descriptionLable)
contentStack.addArrangedSubview(labelStack)
contentStack.addArrangedSubview(iconView)
NSLayoutConstraint.activate([
iconView.heightAnchor.constraint(equalToConstant: 80),
iconView.widthAnchor.constraint(equalToConstant: 80),
contentStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16),
contentStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
contentStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
contentStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16)
])
layoutIfNeeded()
}
However, this code doesn't auto resize the height of the tableview cells, you can see from the screenshot, cells won't expand based on the length of the labels.
What's the best way to do this with stackview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些更改...
现在,您的80pt高度图像视图将
ContentStack
高度限制为80点,因此在您的Content> ContentStack
设置中,请使用:更改您的
labelstack
设置为:确保设置:
edit
这就是它可以通过长而简短的描述以及/不使用图像来看的方式。标签具有青色背景,图像视图具有黄色背景,因此您可以看到框架...
< img src =“ https://i.sstatic.net/nyats.png” alt =“在此处输入图像说明”>
如果这会留下太多的顶部和底部“填充”,则需要调整顶部,并且底部约束常数。
A few changes...
Right now, your 80-pt Height image view is limiting the
contentStack
height to 80-points, so in yourcontentStack
setup, use either:Change your
labelStack
setup to:make sure you set:
Edit
This is how it can look with long and short descriptions, and with / without an image. The labels have a cyan background and the image views have a yellow background so you can see the frames...
If that is leaving too much top and bottom "padding" you need to adjust your top and bottom constraint constants.