即使添加了所有约束,也不会动态计算 UITableViewCell 高度

UITableViewCell height not dynamically being calculated even with all the constraints added

我正在尝试创建具有三个标签的自定义 UItableViewCell。其中两个在左侧(如 built-in 标题 + 副标题布局),另一个在右侧。我正在以编程方式布置标签。

class CustomCell: UITableViewCell {
    static let identifier = String(describing: self)
    
    lazy var primaryLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.preferredFont(forTextStyle: .body)
        return label
    }()
    
    lazy var secondaryLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.preferredFont(forTextStyle: .footnote)
        return label
    }()
    
    lazy var tertiaryLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.preferredFont(forTextStyle: .callout)
        return label
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup() {
        contentView.addSubview(primaryLabel)
        contentView.addSubview(secondaryLabel)
        contentView.addSubview(tertiaryLabel)
        
        NSLayoutConstraint.activate([
            tertiaryLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            tertiaryLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
            tertiaryLabel.heightAnchor.constraint(equalToConstant: 18),
            
            primaryLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            primaryLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 12),
            primaryLabel.heightAnchor.constraint(equalToConstant: 19),

            secondaryLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
            secondaryLabel.topAnchor.constraint(equalTo: primaryLabel.bottomAnchor, constant: 8),
            secondaryLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 12),
//            secondaryLabel.heightAnchor.constraint(equalToConstant: 15)
        ])
    }
}

我想要动态计算单元格的高度。当我实例化 table 视图时,我在视图控制器中将 rowHeightestimatedRowHeight 属性 值设置为 UITableView.automaticDimension

private lazy var tableView: UITableView = {
    let tableView = UITableView(frame: view.bounds, style: .grouped)
    tableView.allowsSelection = false
    tableView.dataSource = self
    tableView.delegate = self
    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = UITableView.automaticDimension
    tableView.register(CustomCell.self, forCellReuseIdentifier: CustomCell.identifier)
    return tableView
}()

但是单元格还是这样显示。使用默认高度。即使我有从上到下布局的自动布局约束。我在控制台中也没有收到任何警告。

知道我在这里遗漏了什么吗?

Demo project

两个观察结果:

  1. 您的辅助标签需要一个负常数到容器底部。

  2. 此外,您估计的行高应该是一些合理的固定值(例如 44 或类似的东西......它不需要是完美的,但只是一些合理的值 table 可用于估计尚未呈现的行的高度)。您 不想 UITableView.automaticDimension 用于 estimatedRowHeight,仅用于 rowHeight