行高不会自动更新

row height is not automatically updated

已编辑:

这是我的自定义单元格 class。它有一个 TextField 和一个 TextView。无论我做什么,我都无法自动更新行高。我知道我可以使用 heightForRowAt 手动完成,但我不想那样做。

class customCell: UITableViewCell, UITextViewDelegate{

var didSetupConstraints = false

    var titleField : UITextField = {
        var textField = UITextField()
        textField.placeholder = "   Subject (optional)"
        textField.backgroundColor = UIColor.lightGray
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 3
        textField.clipsToBounds = true

        return textField
    }()
    var messageView : UITextView = {
        var textView = UITextView()
        textView.text = "Add your email here"
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.backgroundColor = UIColor.red

        return UITextView()
    }()

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

        self.contentView.addSubview(titleField)
        self.contentView.addSubview(messageView)
        messageView.delegate = self
        addConstraints()

    }

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

    private func addConstraints(){
        contentView.addConstraints([titleField.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 23),titleField.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -18),titleField.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 18) ])
        titleField.addConstraint(NSLayoutConstraint(item: titleField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50))

         contentView.addConstraints([messageView.topAnchor.constraint(equalTo: titleField.bottomAnchor, constant: 11),messageView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -18),messageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 18), messageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5)])
     messageView.addConstraint(NSLayoutConstraint(item: messageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))


    }

    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.setNeedsLayout()
        contentView.layoutIfNeeded()
    }

override func updateConstraints() {
    if !didSetupConstraints {
        addConstraints()
        didSetupConstraints = true

    }

    super.updateConstraints()
}


    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = nil
            textView.textColor = UIColor.black
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = "Add your email here"
            textView.textColor = UIColor.lightGray
        }
    }



}

我已经看过 this question,据我了解,我需要做的事情是:

不确定我是否需要做任何其他事情,但我已经完成了所有这三个,但仍然不起作用。我猜也许我的 bottom/top 约束设置不正确。我得到的当前结果是(textView 根本不可见 :(( )

然而我期望得到的是:

编辑 2

见图:

所有修复后,我现在唯一的问题是空单元格没有默认大小44,是tableView试图聪明并根据最后一个调整行高吗单元格高度?

几件事:

  1. updateConstraints 可以被系统调用多次,所以使用标志只在第一次添加约束。
  2. messageView.topAnchor.constraint(equalTo: titleField.topAnchor, constant: 11) 应该是 messageView.topAnchor.constraint(equalTo: titleField.bottomAnchor, constant: 11)
  3. 试着给你的 messageView 一个高度。
  4. 正如@Honey 指出的那样,textViewmessageView 的初始化中没有返回。
  5. 关于空单元格高度,如果您根本不想要空单元格,只需执行 tableView.tableFooterView = UIView() 即可摆脱它们。这可能是 table 视图对单元格高度很聪明,就像你说的那样。