UITableViewCell 未根据其中的图标获取动态单元格大小

UITableViewCell not getting the dynamic cell size based on the icon inside it

我正在以编程方式添加 tableviewcell 及其内容。它有 2 个项目。标签和图像视图。问题是,我正在尝试设置图标高度,但它打破了限制。它在模拟器中显示良好,但我试图找出它打破约束的原因。这是代码:

override func viewDidLoad() {
    super.viewDidLoad()
    setTitle(title: menu.text)
    tableView.register(ListMenuCell.self, forCellReuseIdentifier: "listMenuCell")
    tableView.estimatedRowHeight = 150
    tableView.rowHeight = UITableView.automaticDimension
    setupTableView()
}

//MARK: Setting up tableview
func setupTableView() {
    tableView.translatesAutoresizingMaskIntoConstraints = false        
    view.backgroundColor = UIColor.white
    tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
}

class ListMenuCell: UITableViewCell {

//MARK: - Cell initializers
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
}

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

//MARK: - Cell Properties
let menuLabel:  UILabel = {
    let label = UILabel()
    label.text = ""
    label.lineBreakMode = .byWordWrapping
    label.numberOfLines = 0
    label.font = UIFont.boldSystemFont(ofSize: 17)
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

let menuIcon: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

func setupViews() {
    addSubview(menuLabel)
    addSubview(menuIcon)
    //Note: Don't have to change layoutMarginsGuide to safeAreaLayoutMarginsGuide because its already applied for the table view in which all the below subviews reside in.
    menuIcon.layoutMarginsGuide.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor).isActive = true
    menuIcon.layoutMarginsGuide.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor, constant: 0).isActive = true
    menuIcon.layoutMarginsGuide.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor, constant:0).isActive = true
    menuIcon.layoutMarginsGuide.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
    menuIcon.layoutMarginsGuide.widthAnchor.constraint(equalTo: menuIcon.layoutMarginsGuide.heightAnchor).isActive = true

    menuLabel.layoutMarginsGuide.leadingAnchor.constraint(equalTo: menuIcon.layoutMarginsGuide.trailingAnchor, constant: 20).isActive = true
    menuLabel.layoutMarginsGuide.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
    menuLabel.layoutMarginsGuide.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor, constant: 0).isActive = true
    menuLabel.layoutMarginsGuide.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor, constant:0).isActive = true
}
}

这是破坏约束的日志:

Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x600003790a50 UILayoutGuide:0x600002dcb3a0'UIViewLayoutMarginsGuide'.top == UILayoutGuide:0x600002dcb480'UIViewLayoutMarginsGuide'.top   (active)>",
    "<NSLayoutConstraint:0x600003790aa0 UILayoutGuide:0x600002dcb3a0'UIViewLayoutMarginsGuide'.bottom == UILayoutGuide:0x600002dcb480'UIViewLayoutMarginsGuide'.bottom   (active)>",
    "<NSLayoutConstraint:0x600003790b40 UILayoutGuide:0x600002dcb3a0'UIViewLayoutMarginsGuide'.height == 40   (active)>",
    "<NSLayoutConstraint:0x600003790960 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600002dcb480'UIViewLayoutMarginsGuide']-(11)-|   (active, names: '|':b2cEngageClient.ListMenuCell:0x7f9250667a40'listMenuCell' )>",
    "<NSLayoutConstraint:0x600003791220 'UIView-Encapsulated-Layout-Height' b2cEngageClient.ListMenuCell:0x7f9250667a40'listMenuCell'.height == 62.5   (active)>",
    "<NSLayoutConstraint:0x6000037908c0 'UIView-topMargin-guide-constraint' V:|-(11)-[UILayoutGuide:0x600002dcb480'UIViewLayoutMarginsGuide']   (active, names: '|':b2cEngageClient.ListMenuCell:0x7f9250667a40'listMenuCell' )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003790b40 UILayoutGuide:0x600002dcb3a0'UIViewLayoutMarginsGuide'.height == 40   (active)>

删除这一行

menuIcon.layoutMarginsGuide.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor, constant:0).isActive = true

并替换这个

menuIcon.bottomAnchor.constraint(lessThanOrEqualTo: self.layoutMarginsGuide.bottomAnchor: -16).isActive = true

确保您启用了 table automaticDimension

myTable.rowHeight = UITableView.automaticDimension
myTable.estimatedRowHeight = 100

这是单元格的完整代码

menuIcon.translatesAutoresizingMaskIntoConstraints = false

menuIcon.topAnchor.constraint(equalTo: topAnchor, constant: 16).isActive = true
menuIcon.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
menuIcon.widthAnchor.constraint(equalToConstant: 40).isActive = true
menuIcon.heightAnchor.constraint(equalToConstant: 40).isActive = true
menuIcon.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor,constant: -16).isActive = true


menuLabel.translatesAutoresizingMaskIntoConstraints = false
menuLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
menuLabel.leadingAnchor.constraint(equalTo: menuIcon.trailingAnchor, constant: 16).isActive = true
menuLabel.topAnchor.constraint(equalTo: topAnchor,constant: 16).isActive = true
menuLabel.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -16).isActive = true