如何使用自动调整高度的 SnapKit 以编程方式创建 UITableViewCell?

How to create programatically UITableViewCell using SnapKit that autoresized its height?

看似简单的事情,其实很难。我已经在此处和 SnapKit GitHub 上浏览了多个主题,但未能解决我的问题。

我想要 UITableViewCell 有一个位于中间的标签,比方说单元格顶部和底部的 50。

值得一提的是,单元格是通过编程方式创建的

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.addSubview(titleLabel)
    titleLabel.snp.makeConstraints { (make) -> Void in
        make.topMargin.equalToSuperview().offset(50.0)
        make.left.equalToSuperview().inset(UIView.getValueScaledByScreenWidthFor(baseValue:10.0))
        make.bottomMargin.equalToSuperview().offset(50.0)

    }
}

在 ViewController 中,我尝试了两种自动单元格高度的方法:

 extension EpisodeViewController: UITableViewDelegate {
  func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
  }
}

tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension

viewDidLoad()方法中

我得到的是:

这三个单元格中的每一个都应填充 "Test" - 相反,它已将标签下推到相应单元格下方,而不调整单元格大小。

尝试了许多不同的组合,例如:

1) 将约束优先级设置为 999 - 无变化

2) 添加到 contentView 而不是自己 - 根本不显示

3) 使用 top 而不是 topMargin 等 - 没有区别

你能告诉我这段代码有什么问题吗?在以编程方式创建的单元格中使用 SnapKit 时应根据约束自动调整其高度的一般经验法则是什么?

提前致谢

编辑

UITableView 数据源方法

extension EpisodeViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: EpisodeHeaderCell = tableView.dequeueReusableCell(for: indexPath)
    cell.viewModel = viewModel
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

}

UITableViewCell 有 contentView 应该用于添加自定义视图

所以尝试这样的事情(我还没有测试过)

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

    self.contentView.addSubview(titleLabel)

    titleLabel.snp.makeConstraints { (make) -> Void in
        // Changed from bottom to top
        make.top.equalTo(self.contentView.snp.top).offset(50)
        make.bottom.equalTo(self.contentView.snp.bottom).offset(50)
        make.left.equalTo(self.contentView.snp.left)
        make.right.equalTo(self.contentView.snp.right)
    }
}
    label.snp.makeConstraints {
        [=10=].left.equalToSuperview().offset(10)
        [=10=].right.equalToSuperview().offset(-10)
        [=10=].top.equalToSuperview().offset(50)
        [=10=].bottom.equalToSuperview().offset(-50)
    }

这里是viewController.swift文件的全部代码。

class TestCell: UITableViewCell {
    static let identifier: String = "test_cell_identifier"

    var label: UILabel!

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

    func configure() {
        label = UILabel(frame: .zero)
        self.contentView.addSubview(label)
        label.snp.makeConstraints {
            [=11=].left.equalToSuperview().offset(10)
            [=11=].right.equalToSuperview().offset(-10)
            [=11=].top.equalToSuperview().offset(50)
            [=11=].bottom.equalToSuperview().offset(-50)
        }
    }

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

class ViewController: UIViewController {
    var data: [String] = [
        "Test1",
        "Test2",
        "Test3"
    ]

    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView = UITableView(frame: .zero)
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints {
            [=11=].edges.equalToSuperview()
        }
        tableView.register(TestCell.self, forCellReuseIdentifier: TestCell.identifier)
        tableView.dataSource = self
        tableView.delegate = self

        tableView.estimatedRowHeight = 100
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TestCell.identifier, for: indexPath) as! TestCell
        cell.label.text = data[indexPath.item]
        return cell
    }
}