【发布时间】:2019-03-25 06:07:48
【问题描述】:
我正在创建一个自定义 UITableViewCell。目前,我只想让它在左侧有一个UIButton (checkButton),在按钮的右侧有两个UILabels(titleLabel 和notesLabel)。
基本上,它应该看起来像一个标准的UITableViewCell,带有一个图像和两个文本标签(但请不要告诉我只是重复使用一个标准单元格,因为由于各种原因我不能这样做)。该按钮应具有固定大小 (16x16) 并在单元格中垂直居中。这两个标签应换行并展开以适合其内容。我正在尝试以编程方式定义此单元格,因此我创建了以下初始化程序来定义约束。
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.font = UIFont.systemFont(ofSize: 16)
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.numberOfLines = 0
contentView.addSubview(titleLabel)
checkButton.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(checkButton)
notesLabel.translatesAutoresizingMaskIntoConstraints = false
notesLabel.font = UIFont.systemFont(ofSize: 13)
notesLabel.lineBreakMode = .byWordWrapping
notesLabel.numberOfLines = 0
contentView.addSubview(notesLabel)
addConstraint(NSLayoutConstraint(item: titleLabel,
attribute: .top,
relatedBy: .equal,
toItem: contentView,
attribute: .top,
multiplier: 1,
constant: 0))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .top,
relatedBy: .equal,
toItem: titleLabel,
attribute: .bottom,
multiplier: 1,
constant: 0))
addConstraint(NSLayoutConstraint(item: titleLabel,
attribute: .trailing,
relatedBy: .equal,
toItem: contentView,
attribute: .trailing,
multiplier: 1,
constant: -10))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .trailing,
relatedBy: .equal,
toItem: contentView,
attribute: .trailing,
multiplier: 1,
constant: -10))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .bottom,
relatedBy: .equal,
toItem: contentView,
attribute: .bottom,
multiplier: 1,
constant: 0))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .leading,
relatedBy: .equal,
toItem: contentView,
attribute: .leading,
multiplier: 1,
constant: 20))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .centerY,
relatedBy: .equal,
toItem: contentView,
attribute: .centerY,
multiplier: 1,
constant: 0))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 0,
constant: 16))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 0,
constant: 16))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .leading,
relatedBy: .equal,
toItem: checkButton,
attribute: .trailing,
multiplier: 1,
constant: 12))
addConstraint(NSLayoutConstraint(item: titleLabel,
attribute: .leading,
relatedBy: .equal,
toItem: checkButton,
attribute: .trailing,
multiplier: 1,
constant: 12))
}
当我运行这段代码时,它大部分都按预期工作,除了 Xcode 打印以下警告:[Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
我通常会忽略这一点,但它似乎阻止了单元格扩展以适应其内容。例如,如果其中一个标签有足够的内容扩展到 3 行,则仅显示第一行。我想要的行为是让标签(以及扩展的单元格)扩展以适应它们的内容。我在高度限制方面做错了什么?
【问题讨论】:
-
约束应该添加到 contentView。
-
啊,谢谢!我现在可以停止拔头发了。如果您将其发布为答案,我会接受。
标签: ios swift uitableview autolayout nslayoutconstraint