【问题标题】:Ambiguous UILabel height in autoresizing UITableViewCell自动调整 UITableViewCell 大小时不明确的 UILabel 高度
【发布时间】:2021-10-27 07:17:06
【问题描述】:

我很难通过自动调整单元格大小来实现 UITableView。我正在尝试使用具有自动大小的单元格内带有 2 个标签的 UIStackView。

总体上看起来不错,但我遇到了以下运行时问题:

我觉得我所拥有的约束对于这里的用例来说应该足够了,但我想摆脱这里的歧义。

如何实现?这里常用的方法是什么?

我已经准备了一个示例项目,它简化了我的用例,但仍然演示了这个问题。这是我的控制器:

class ViewController: UIViewController {

    struct CellData {
        let title: String
        let subtitle: String
    }

    let table = UITableView()

    let data = [
        CellData(title: "Foo", subtitle: "Bar"),
        CellData(title: "Baz", subtitle: "FooBar")
    ]

    private let cellReuseID = "CellReuseID"

    override func viewDidLoad() {
        super.viewDidLoad()

        table.register(Cell.self, forCellReuseIdentifier: cellReuseID)
        table.dataSource = self
        table.rowHeight = UITableView.automaticDimension
        table.tableFooterView = UIView(frame: .zero)

        table.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(table)
        NSLayoutConstraint.activate([
            table.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            table.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            table.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            table.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

        ])

    }
}

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseID) as? Cell else {
            fatalError()
        }
        cell.title = data[indexPath.row].title
        cell.subtitle = data[indexPath.row].subtitle
        return cell
    }
}

以及它使用的单元格:

class Cell: UITableViewCell {

    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }

    var subtitle: String? {
        didSet {
            subtitleLabel.text = subtitle
        }
    }

    let titleLabel = UILabel()

    let subtitleLabel = UILabel()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupView()
    }

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

    private func setupView() {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.alignment = .top
        stackView.distribution = .fill
        stackView.spacing = 8

        stackView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(stackView)
        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            stackView.topAnchor.constraint(equalTo: contentView.topAnchor)
        ])


        stackView.addArrangedSubview(titleLabel)
        stackView.addArrangedSubview(subtitleLabel)
    }
}

【问题讨论】:

    标签: ios uitableview uikit uistackview uitableviewautomaticdimension


    【解决方案1】:

    Ambiguous Height 的原因是因为两个标签具有相同的 Content Hugging Priority。

    自动布局进行“最佳猜测”,您会得到所需的输出 - 但您仍会在 Debug View Hierarchy 中看到问题。

    为了摆脱它,你可以给一个标签一个更高的拥抱优先级。

    例如(setupView()):

        titleLabel.setContentHuggingPriority(.defaultHigh + 1, for: .vertical)
        subtitleLabel.setContentHuggingPriority(.defaultHigh, for: .vertical)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 2010-11-03
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      相关资源
      最近更新 更多