【发布时间】:2017-06-04 19:38:26
【问题描述】:
我有一个包含 UILabel 的单元格的 UITableView。 UILabel 有一个自定义的 UIEdgeInset。我将 UILabel 子类化并像这样设置 UIEdgeInsets:
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.width += leftInset + rightInset
contentSize.height += topInset + bottomInset
return contentSize
}
但是当我在 UILabel 中有更多行时,标签有时会被截断。 我已经将行高配置为 UITableViewAutomaticDimension 并设置了 estimatedRowHeight。约束也很好。 问题似乎出在我设置 UIEdgeInsets 时,因为如果我不自定义它,它就可以正常工作。
可能我应该告诉单元格在设置插图后更新约束,但到目前为止我还不能这样做。
故事板中添加的约束。 Bottom、Top、Leading 和 Trailing 与超级视图(UITableViewCell)相关。所有常量设置为 0。
cellForRowAtIndexPath中的代码如下:
let cell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell", for: indexPath) as! AnswerCell
cell.answerLabel.text = alternatives[indexPath.row]
return cell
【问题讨论】:
-
卢卡斯你是说标签只有在标签中有不止一行时才会被截断?通过截断我假设您的意思是被单元格分隔器水平切断 - 即单元格没有正确扩展以容纳标签中的额外行。对吗?
-
你的标签子类对我来说是正确的,因为它具有自我调整大小的表格视图单元格,所以你可能想要分享你用来设置表格视图单元格和子视图的代码。
-
@thecloud_of_unknowing 是的,字符串不适合视图。当我有不止一条线时,它不会一直发生。似乎没有考虑我设置的新 UIEdgeInsets 。所以在某些情况下它很合适,但在其他情况下它可能会被截断,因为它希望有更多的宽度空间。
-
@jamesk 除此之外没有太多代码。在
viewDidLoad中,我将rowHeight设置为tableView.rowHeight = UITableViewAutomaticDimension,同时将estimatedRowHeight设置为tableView.estimatedRowHeight = 120。而在cellForRowAtIndexPath我只设置了标签文字。 -
刚刚添加了更多信息,但看起来很简单,与问题无关。
标签: ios swift uitableview autolayout uilabel