UITableViewAutomaticDimension的工作方式与预期不符.迅速

Yur*_*hin 1 uitableview ios swift uitableviewautomaticdimension

阅读雷Wenderlich后指导的"自调整大小表格视图单元",以及这个问题和答案,我已经决定要问大家的帮助.

有一个程序创建的单元格:

import UIKit

class NotesCell: UITableViewCell {
lazy private var cellCaption: UILabel = {
    let label = UILabel()

    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping

    return label
}()

func configure(with note: NotesModel) {
    cellCaption.text = note.name

    contentView.addSubview(cellCaption)
}

override func layoutSubviews() {
    super.layoutSubviews()

    NSLayoutConstraint.activate([
        cellCaption.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
        cellCaption.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
        cellCaption.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
//            cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
cellCaption.bottomAnchor.constraint(greaterThanOrEqualTo: contentView.bottomAnchor, constant: -8)
            ])

//        cellCaption.sizeToFit()
//        cellCaption.layoutIfNeeded()
}
}
Run Code Online (Sandbox Code Playgroud)

表视图控制器在委托方法中使用UITableViewAutomaticDimension:

extension NotesTableViewController {
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)

}

结果,最长的标题被完全指示,但无论如何,单元格具有与所有其他标题相同的高度.

在此输入图像描述

一些更新!

我已经尝试将以下代码放入viewDidLoad():

tableView.rowHeight = 44
tableView.estimatedRowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)

启用委托方法并禁用它们.结果是一样的 :(

Tee*_*etz 6

我建议不要使用Delegate-Methods来满足您的需求.

只需尝试在您的设置viewDidLoad:

self.tableView.rowHeight = UITableViewAutomaticDimension;
// set estimatedRowHeight to whatever is the fallBack rowHeight
self.tableView.estimatedRowHeight = 44.0;
Run Code Online (Sandbox Code Playgroud)

这对我来说总是有用的.让我知道它是否有帮助:)


Don*_*Mag 5

您做错了很多事情,但要点您对greaterThanOrEqualTo:.

相反,它应该是:

cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
Run Code Online (Sandbox Code Playgroud)

此外,每次设置文本时,当前代码都会添加一个新标签作为子视图。单元格是重复使用的,因此您只需在创建单元格时添加标签。

接下来,该表的正确属性是:

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44
Run Code Online (Sandbox Code Playgroud)

将这两行放入viewDidLoad()表视图控制器中,并且实现heightForRowAtestimatedHeightForRowAt函数。你可以extension完全删除你的。

最后,您只需设置一次约束。绝对不是layoutSubviews().

这是一个完整的示例:

//
//  NotesTableViewController.swift
//
//  Created by Don Mag on 8/29/18.
//

import UIKit

class NotesModel: NSObject {
    var name: String = ""
}

class NotesCell: UITableViewCell {
    lazy private var cellCaption: UILabel = {
        let label = UILabel()

        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.medium)
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping

        return label
    }()

    func configure(with note: NotesModel) {
        cellCaption.text = note.name
    }

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() -> Void {

        contentView.addSubview(cellCaption)

        NSLayoutConstraint.activate([
            cellCaption.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
            cellCaption.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            cellCaption.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
            cellCaption.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
            ])

    }

}

class NotesTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotesCell", for: indexPath) as! NotesCell

        let m = NotesModel()

        if indexPath.row == 3 {
            m.name = "This is a very long caption. It will demonstrate how the cell height is auto-sized when the text is long enough to wrap to multiple lines."
        } else {
            m.name = "Caption \(indexPath.row)"
        }

        cell.configure(with: m)

        return cell
    }

}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述