如何在Uitable视图单元格中动态调整Uilabel?
我在我的UitableView(如下所示)中定义了一种CustomCell类型,该类型在StackView中包含两个标签。 StackView中的第二个标签将其数字线设置为2。但是,当该行具有1行数据时,它会导致第二个标签垂直切除(如下所示)。
如何更新约束,以便第二个标签可以根据有1或2行数据而动态调整?
我尝试的是:
- 将stackView的分布设置为 fillibletally ,仅当第二个标签中有2行数据
- 添加 spacing 在stackView中添加2行数据时,才能分开两个
- 标签stackView,并分别约束两个标签
struct Data {
var type, name: String
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var data = [Data]()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = .white
tableView.separatorStyle = .none
return tableView
}()
func setupView() {
data = [
Data(type: "Breakfast", name: "Poached Egg & Avocado Toast with Sliced Cherry Tomatoes"),
Data(type: "Lunch", name: "Poached Egg & Avocado Toast")
]
view.backgroundColor = .white
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
let data = data[indexPath.row]
cell.configure(with: data)
cell.backgroundColor = .white
return cell
}
}
class CustomCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0))
}
lazy var typeLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 25, weight: .bold)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 20)
label.numberOfLines = 2
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var titleStack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [typeLabel, nameLabel])
stack.axis = .vertical
stack.distribution = .fillProportionally
stack.translatesAutoresizingMaskIntoConstraints = false
return stack
}()
lazy var iv: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.translatesAutoresizingMaskIntoConstraints = false
iv.backgroundColor = .systemMint
return iv
}()
func configure(with mealInfo: Data) {
typeLabel.text = mealInfo.type.capitalized
nameLabel.text = mealInfo.name
iv.image = UIImage(systemName: "star")
}
func setupView() {
contentView.backgroundColor = .systemPurple
contentView.addSubview(titleStack)
contentView.addSubview(iv)
NSLayoutConstraint.activate([
titleStack.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 10),
titleStack.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.9)
])
NSLayoutConstraint.activate([
iv.topAnchor.constraint(equalTo: titleStack.bottomAnchor, constant: 10),
iv.leftAnchor.constraint(equalTo: titleStack.leftAnchor),
iv.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.55),
iv.heightAnchor.constraint(equalTo: iv.widthAnchor, multiplier: 0.6),
iv.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10)
])
}
}
I defined a CustomCell type in my UITableView (shown below), which contains two labels in a stackView. The second label in the stackView has its numberOfLines set to 2. However, when this line has 1 line of data, it causes the second label to be cut off vertically (shown below).
How can I update the constraints so that the second label can dynamically adjust based on if there are 1 or 2 lines of data?
What I tried:
- Setting the stackView's distribution to fillProportionally, this only works if there are 2 lines of data in the second label
- Adding spacing to the stackView to separate the two labels
- Removing the stackView, and constraining the two labels individually
struct Data {
var type, name: String
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var data = [Data]()
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = .white
tableView.separatorStyle = .none
return tableView
}()
func setupView() {
data = [
Data(type: "Breakfast", name: "Poached Egg & Avocado Toast with Sliced Cherry Tomatoes"),
Data(type: "Lunch", name: "Poached Egg & Avocado Toast")
]
view.backgroundColor = .white
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.9),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
let data = data[indexPath.row]
cell.configure(with: data)
cell.backgroundColor = .white
return cell
}
}
class CustomCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0))
}
lazy var typeLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 25, weight: .bold)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 20)
label.numberOfLines = 2
label.lineBreakMode = .byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var titleStack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [typeLabel, nameLabel])
stack.axis = .vertical
stack.distribution = .fillProportionally
stack.translatesAutoresizingMaskIntoConstraints = false
return stack
}()
lazy var iv: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.translatesAutoresizingMaskIntoConstraints = false
iv.backgroundColor = .systemMint
return iv
}()
func configure(with mealInfo: Data) {
typeLabel.text = mealInfo.type.capitalized
nameLabel.text = mealInfo.name
iv.image = UIImage(systemName: "star")
}
func setupView() {
contentView.backgroundColor = .systemPurple
contentView.addSubview(titleStack)
contentView.addSubview(iv)
NSLayoutConstraint.activate([
titleStack.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
titleStack.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 10),
titleStack.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.9)
])
NSLayoutConstraint.activate([
iv.topAnchor.constraint(equalTo: titleStack.bottomAnchor, constant: 10),
iv.leftAnchor.constraint(equalTo: titleStack.leftAnchor),
iv.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.55),
iv.heightAnchor.constraint(equalTo: iv.widthAnchor, multiplier: 0.6),
iv.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10)
])
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)