gpt4 book ai didi

ios - UITableViewCell 中的 UILabel 意外剪切

转载 作者:行者123 更新时间:2023-11-29 05:29:56 25 4
gpt4 key购买 nike

我有一个 UITableViewCell 子类,完全用代码编写(没有 xib/storyboard),它只是添加一些要像在聊天 View 中一样显示的文本。

它工作正常,只是缺少最后一个单词。显然已经被截断了。

更改字符串会改变剪辑发生的位置,但它似乎总是被删除的最后一行。

这些限制在我看来是正确的。谁能指出这里出了什么问题吗?

class MessageCell : UITableViewCell {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let label = UILabel()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

let spacer = UIView()

label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
label.textAlignment = .right
label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0

backgroundColor = .clear

let stackView = UIStackView(arrangedSubviews: [spacer, label])
stackView.distribution = .fill
stackView.axis = .horizontal
stackView.spacing = 0
stackView.alignment = .trailing
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)

NSLayoutConstraint.activate([
stackView.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
stackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
stackView.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor),
stackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor),
spacer.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.2, constant: 1.0)
])
}
}

已注册:

tableView.register(MessageCell.self, forCellReuseIdentifier: "messageCell")

并返回:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "messageCell", for: indexPath)
}

这是我所看到的屏幕截图(iOS 12 iPhoneX 模拟器):

enter image description here

最佳答案

您可以使用

optional func tableView(_ tableView: UITableView, 
heightForRowAt indexPath: IndexPath) -> CGFloat

方法并尝试增加单元格本身的大小。我认为这是造成问题的原因。否则,您也可以使用自动调整单元格大小。顺便说一句,用户使用了错误的重用标识符(消息而不是 messageCell)。但我不认为这会导致上述问题。

关于ios - UITableViewCell 中的 UILabel 意外剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57716518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com