UITableView 高度在 运行 时间内通过 SnapKit 的按钮操作没有改变

UITableView height is not changing in run time by button action with SnapKit

当我尝试更改 table 视图的高度时,控制台出现以下错误。

[LayoutConstraints] Unable to simultaneously satisfy constraints.   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.  (
    "<SnapKit.LayoutConstraint:0x6000014e40c0@AddTravellerViewController.swift#49 UITableView:0x7ff246001200.height == 60.0>",
    "<SnapKit.LayoutConstraint:0x6000014e5860@AddTravellerViewController.swift#49 UITableView:0x7ff246001200.height == 120.0>" )

UIButton 操作:我是第一次在 viewdidload 中加载此功能。从第二次开始它打印上面的错误信息。

func addButtonAction(){

    travellersCount! += 1
    tableHeight! += 60

    tableView.snp.makeConstraints { (make) in
        make.left.equalTo(self.view).offset(10)
        make.right.equalTo(self.view).offset(-10)
        make.top.equalTo(self.view).offset(120)
        make.height.equalTo(tableHeight!)
    }

    addButotn.snp.makeConstraints { (make) in
        make.left.right.equalTo(self.view)
        make.height.equalTo(40)
        make.top.equalTo(tableView.snp.bottom).offset(10)
    }

    tableView.reloadData()
}

表格视图

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Textcell", for: indexPath) as! TextfieldTableViewCell
    cell.backgroundColor = .clear
    return cell

}

set heightForRow UITableView.automaticDimension 会根据内容自动设置行高

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

您收到错误是因为您已经通过 SnapKitmakeConstraints 方法为您的 UITableView 声明了高度限制,并且您的 addButtonAction() 应该得到多次调用以保持 'creating' 具有更大值的冲突高度限制

嗯,viewDidLoad 通常不会被调用超过一次,但最好不要假设它并在其中进行一次性配置。如果您从 nib 文件实例化,请考虑在 awakeFromNib...

中执行此操作

现在,在viewDidLoad里面解决这个问题:-

1) 如果你希望这是一个一次性的过程,你可以创建一个 属性(类型 Constraint,这是 SnapKit 的自定义对象类型)来保存对的引用您在 tableView.snp.makeConstraints 中声明的高度约束并进行 nil 检查:

Delcare a 属性 以上:

private var tableViewHeightConstraint: Constraint?

并进行 nil 检查:

tableView.snp.makeConstraints { (make) in
    make.left.equalTo(self.view).offset(10)
    make.right.equalTo(self.view).offset(-10)
    make.top.equalTo(self.view).offset(120)

    if self.tableViewHeightConstraint == nil {
       self.tableViewHeightConstraint = make.height.equalTo(tableHeight!)
    }
}

2) 或者,如果您想每次都更新高度(当您将 tableHeight 增加 60 时),您可以使用 updateConstraints:

tableView.snp.updateConstraints { (make) in
    //Update your height constraint here. Note: This will keep increasing the height of your table on every call
    make.height.equalTo(tableHeight!)
}

(您可能必须在此之后调用 layoutIfNeeded(),才能进行更改)

我还没有测试过这段代码,但这应该适合你。如果没有,请告诉我...