首页 > 解决方案 > Swift 不要在自定义单元格中工作自动高度

问题描述

只是单元格的自定义类

class CarCell: UITableViewCell { 

override func layoutSubviews() {

    MainImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: spacing * 5).isActive = true
    MainImageView.topAnchor.constraint(equalTo: self.topAnchor, constant: spacing).isActive = true
    MainImageView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -spacing * 5 ).isActive = true
    MainImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -spacing).isActive = true
    MainImageView.heightAnchor.constraint(equalTo: MainImageView.widthAnchor).isActive = true

}

不要工作 2 底线。

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(CarCell.self, forCellReuseIdentifier: "CarCell")
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44
}

在此处输入图像描述

我可以创建这个非程序化的,我需要这个结果

在此处输入图像描述

标签: swiftcell

解决方案


您需要在单元格中添加约束:

底部、顶部、左侧、右侧和高度(您可以大于 >= 或小于 <=)

还添加clipToBounds = trueimage view

在此处输入图像描述


推荐阅读