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

Ios 如何使用自动调整高度的SnapKit以编程方式创建UITableViewCell?,ios,swift,uitableview,autolayout,snapkit,Ios,Swift,Uitableview,Autolayout,Snapkit,看似简单的事情,结果却变得很难。我已经在这里和SnapKit GitHub上浏览了多个主题,但未能解决我的问题 < P> >我想有 UITabelVIEW CULL 一个位于中间的标签,从顶部和底部50个都表示。 值得一提的是,单元格是通过编程方式创建的 override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuse

看似简单的事情,结果却变得很难。我已经在这里和SnapKit GitHub上浏览了多个主题,但未能解决我的问题

< P> >我想有<代码> UITabelVIEW CULL 一个位于中间的标签,从顶部和底部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
  }
}

viewDidLoad()方法中

我得到的是:

这三个单元格中的每一个都应该填充“Test”-相反,它将标签向下推到了相应单元格的下方,而没有调整单元格的大小

尝试了多种组合,例如:

1) 将约束优先级设置为999-无更改

2) 添加到contentView而不是self根本不会显示

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

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

提前谢谢

编辑

UITableView
DataSource方法

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
,应用于添加自定义视图

所以试试这样的方法(我还没有测试过)

以下是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 {
            $0.left.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
            $0.top.equalToSuperview().offset(50)
            $0.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 {
            $0.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
    }
}

您是否尝试为UILabel提供大小为的字体?@EmreÖnder是的,我没有粘贴这部分代码,但您可以看到字体大小大于系统。您可以从
cellForRowAt
发布代码吗?@CerlinBoss当然,请查看我编辑的问题,谢谢。@DCDC抱歉代码中有错误。很有可能现在就可以了。我已经添加了对我所做的更改的评论,明白了,但现在它看起来与原始帖子中的图片几乎相同-标签超出了单元格,这仍然没有更改谢谢,但我认为它与ViewModel无关,也没有通过问题标题中解释的故事板添加它。你尝试过这个吗?cell.layoutIfNeeded()我回答了所有可能的例子来帮助您解决它。cell.layoutIfNeeded()应该可以工作。为什么你给我的答案加了一个
减号
?这并不不幸,我可以取消这次否决投票,但请编辑你的答案以匹配问题(没有故事板)@LiJin-你的代码有效,但有几个问题。。。1)
self.addSubview(标签)
应该是
contentView.addSubview(标签)
。。。2)
tableView.rowHeight=UITableView.automaticDimension是默认值,因此不需要。。。3) 
tableView.EstimatedRowheaight
不应为
UITableView.automaticDimension
-应为估计值,在这种情况下,120是合理的。。。4) 不要调用cell.layoutifneed()
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 {
        $0.left.equalToSuperview().offset(10)
        $0.right.equalToSuperview().offset(-10)
        $0.top.equalToSuperview().offset(50)
        $0.bottom.equalToSuperview().offset(-50)
    }
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 {
            $0.left.equalToSuperview().offset(10)
            $0.right.equalToSuperview().offset(-10)
            $0.top.equalToSuperview().offset(50)
            $0.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 {
            $0.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
    }
}