Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过使用SnapKit的按钮操作,UITableView高度在运行时不会更改_Ios_Swift_Uitableview_Snapkit - Fatal编程技术网

Ios 通过使用SnapKit的按钮操作,UITableView高度在运行时不会更改

Ios 通过使用SnapKit的按钮操作,UITableView高度在运行时不会更改,ios,swift,uitableview,snapkit,Ios,Swift,Uitableview,Snapkit,当我试图更改表视图的高度时,控制台中出现以下错误 [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 whi

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

[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>" )
桌面视图

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

}
设置行的高度UITableView.automaticDimension它将根据内容自动设置行的高度

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

出现此错误是因为您已经通过SnapKit的makeConstraints方法为UITableView声明了高度约束,并且您的addButtonAction可能会被多次调用,以继续“创建”具有更大值的冲突高度约束

嗯,viewDidLoad通常不会被调用多次,但最好不要假设这样,并在其中进行一次性配置。如果从NIB文件中实例化,请考虑在AuxeFiNib……/P>内执行此操作。 现在,要解决此问题,请执行以下操作:-

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

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!)
    }
}
Delcare a上述物业:

private var tableViewHeightConstraint: Constraint?
并进行零检查:

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时更新高度,可以使用UpdateConstraint:

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


我还没有测试过这段代码,但这应该可以帮到你。如果没有,请告诉我……

与所问问题无关。
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!)
}