我正在创建一个自定义UITableViewCell
。现在,我想要的只是左边有一个UIButton
(checkButton
),按钮右边有两个UILabel
s(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行,则只显示第一行。我想要的行为是扩展标签(以及扩展名,单元格)以适应其内容。我对高度限制做错了什么?
注意:请在Cell中添加标签和按钮,而不是在单元格的contentView中。下面的代码将工作,请检查并告诉我任何问题。
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(titleLabel)
addSubview(checkButton)
addSubview(notesLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.font = UIFont.systemFont(ofSize: 16)
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.numberOfLines = 0
checkButton.translatesAutoresizingMaskIntoConstraints = false
notesLabel.translatesAutoresizingMaskIntoConstraints = false
notesLabel.font = UIFont.systemFont(ofSize: 13)
notesLabel.lineBreakMode = .byWordWrapping
notesLabel.numberOfLines = 0
addConstraint(NSLayoutConstraint(item: titleLabel,
attribute: .top,
relatedBy: .equal,
toItem: self,
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: self,
attribute: .trailing,
multiplier: 1,
constant: -10))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .trailing,
relatedBy: .equal,
toItem: self,
attribute: .trailing,
multiplier: 1,
constant: -10))
addConstraint(NSLayoutConstraint(item: notesLabel,
attribute: .bottom,
relatedBy: .equal,
toItem: self,
attribute: .bottom,
multiplier: 1,
constant: 0))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .leading,
relatedBy: .equal,
toItem: self,
attribute: .leading,
multiplier: 1,
constant: 20))
addConstraint(NSLayoutConstraint(item: checkButton,
attribute: .centerY,
relatedBy: .equal,
toItem: self,
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))
}
这是我的方法
private func setUp() {
self.selectionStyle = .none
self.contentView.backgroundColor = .white
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
button.setImage(UIImage(named: "arrow"), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(button)
button.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8.0).isActive = true
button.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: button.frame.width).isActive = true
button.heightAnchor.constraint(equalToConstant: button.frame.height).isActive = true
let view = UIView()
view.backgroundColor = .red
view.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(view)
view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 8.0).isActive = true
view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: button.frame.width + 16.0).isActive = true
view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 8.0).isActive = true
view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -8.0).isActive = true
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.numberOfLines = 0
view.addSubview(titleLabel)
titleLabel.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
notesLabel.translatesAutoresizingMaskIntoConstraints = false
notesLabel.numberOfLines = 0
view.addSubview(notesLabel)
notesLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8.0).isActive = true
notesLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
notesLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
notesLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
如果标签是多线动态高度,请使用以下命令启用自调整单元格
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 44.0
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
产量