Ios UITableViewCell中的UILabel意外剪裁
我有一个UITableViewCell子类,完全是代码(没有xib/storyboard),它只是添加一些文本来显示,就像在聊天视图中一样 它工作正常,只是缺少最后一个字。它显然被截断了 更改字符串会更改剪辑发生的位置,但它似乎总是最后一行被删除 这些约束条件对我来说很合适。有人能告诉我这里出了什么问题吗Ios UITableViewCell中的UILabel意外剪裁,ios,uitableview,Ios,Uitableview,我有一个UITableViewCell子类,完全是代码(没有xib/storyboard),它只是添加一些文本来显示,就像在聊天视图中一样 它工作正常,只是缺少最后一个字。它显然被截断了 更改字符串会更改剪辑发生的位置,但它似乎总是最后一行被删除 这些约束条件对我来说很合适。有人能告诉我这里出了什么问题吗 class MessageCell : UITableViewCell { required init?(coder aDecoder: NSCoder) { fata
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模拟器):
您可以使用
optional func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
方法并尝试增大单元格本身的大小。我认为这是造成问题的原因。否则,也可以使用自动调整单元格大小。顺便说一下,use使用了错误的重用标识符(message而不是messageCell)。但我不认为这是造成上述问题的原因。结果表明,这在独立项目中效果不错,但在我正在进行的项目中效果不佳。我还不明白为什么。但我确实找到了如何做到这一点:不使用堆栈视图
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)
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
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(label)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: -8),
label.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -16),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8)
])
contentView.addConstraint(NSLayoutConstraint(item: label, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 0.2, constant: 0))
}
}
我看不出有任何理由在另一个不起作用的情况下这样做。这是一个自动调整大小的单元格。不正确的重用标识符是我在准备StackOverflow帖子时犯的一个错误-我会纠正它-我测试的代码是正确的。我可以返回到计算大小并返回它,但我相信我在这里所做的方式应该是可行的,并且会比这简单得多。好奇出了什么问题。正如你所发布的,它在新项目中运行良好。但是,我很好奇。。。为什么在堆栈视图中使用“间隔”视图?为什么不简单地将标签约束到后缘,并将标签宽度约束到contentView宽度的80%--
label.widthAnchor.constraint(equalTo:contentView.layoutMarginsGuide.widthAnchor,乘数:0.8)
?我对堆栈视图的理解是,它们应该为您处理定位约束-在标签上放置一个垫片,将其约束到标签之外的某个位置,我觉得不合适。间隔棒感觉更干净。堆栈视图非常适合排列多个视图。在你的情况下,它并不真正适用。当然,有多种方法可以获得您想要的布局,但简单性通常是更好的方法。我会这样做: