我正在使用自动布局和原型单元创建UITableView。我希望它的行为更像一个Web HTML表。每个单元格都有四个“列”,每个列中都有一个标签,
see here。最终,每列应将其宽度调整为所有其他行中最宽的列。这似乎很普通,但是我找不到任何有关它的信息。我在这里错过明显的东西吗?
我现在有什么结果:
1|product name|11 xx|22 yy
1|longer product name|11 xxx|22 yy
1|short|1x|22 yy
我想要的是:
1|product name |11 xx |22 yy
1|longer product name|11 xxx|22 yy
1|short |1x |22 yy
我希望这有任何意义,否则请通知我。谢谢!
最佳答案
您可以使用嵌入式UIStackView
构建标签的“网格”
使用“外部”水平堆栈视图可容纳4个“内部”堆栈视图,这些视图用作4个“列”。
如果内容超出可用区域,则将外部堆栈视图嵌入UIScrollView
中。
结果看起来像这样:
前两个“列”堆栈视图具有.alignment = .leading
,而第3个和第4个具有.alignment = .trailing
。如您所见,每个“列”堆栈视图都会扩展其宽度以适合最宽的标签。
这是产生该示例的一些代码:
class StackGridViewController: UIViewController {
var myData = [[String]]()
override func viewDidLoad() {
super.viewDidLoad()
// init 30 rows of data
for i in 1...30 {
myData.append(["\(i)", "Product Name", "12 xx", "34 xx"])
}
// make a few of the rows different
for i in [2, 15, 21] {
// longer "column B"
myData[i][1] = "Longer Product Name"
// longer "column C"
myData[i+2][2] = "1234 xx"
// longer "column D"
myData[i+4][3] = "3456 xx"
}
// and one row with the longest values
// longest "column B"
myData[9][1] = "The Longest Product Name"
// longest "column C"
myData[9][2] = "123456 xx"
// longest "column D"
myData[9][3] = "345678 xx"
// create a horizontal stack view
let outerSV = UIStackView()
outerSV.translatesAutoresizingMaskIntoConstraints = false
outerSV.axis = .horizontal
outerSV.distribution = .fill
// "column" spacing
outerSV.spacing = 12
// add 4 "column" vertical stack views
for col in 0..<4 {
let columnSV = UIStackView()
columnSV.translatesAutoresizingMaskIntoConstraints = false
columnSV.axis = .vertical
// align leading for first two columns, trailing for 3rd and 4th
columnSV.alignment = col < 2 ? .leading : .trailing
columnSV.distribution = .fill
// "row" spacing
columnSV.spacing = 8
// add a label for each data "row"
for row in 0..<myData.count {
let v = UILabel()
v.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
v.font = UIFont.systemFont(ofSize: 13.0, weight: .light)
v.text = myData[row][col]
columnSV.addArrangedSubview(v)
}
// add the "column" stack view to the outer stack view
outerSV.addArrangedSubview(columnSV)
}
// create a scroll view
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
// constrain it to all four sides with 20-pt padding on top/bottom, 8-pt padding leading/trailing
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20.0),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20.0),
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 8.0),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -8.0),
])
// add the outer stack view to the scroll view
scrollView.addSubview(outerSV)
// constrain it to all four sides (which will also define .contentSize (the "scrollable area")
NSLayoutConstraint.activate([
outerSV.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0.0),
outerSV.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0.0),
outerSV.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0.0),
outerSV.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0.0),
])
}
}
关于ios - UITableViewCell对齐列以适合内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55301591/