首页 > 解决方案 > 无法同时满足从 ios 13 中的 nib 加载的 tableview 单元格的约束

问题描述

我有一个从 nib 文件加载的 tableview 和自定义单元格,这个单元格有一个高度约束(在我的例子中是 80.0),当我启动应用程序时,Xcode 说有一个约束错误。我发现只有当 tableview 有分隔符时才会出现这个问题。如果我想同时拥有单元格的高度约束和表格视图中的分隔符,有没有办法解决这个约束错误?

带表视图的 ViewController

class ViewController: UIViewController, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "SampleTableViewCell", bundle: nil), forCellReuseIdentifier: "SampleTableViewCell")
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SampleTableViewCell", for: indexPath) as! SampleTableViewCell
        return cell
    }

}

我得到的错误

2019-10-13 18:08:04.345180+0300 tableViewProblem[5281:391449] [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. 
(
    "<NSLayoutConstraint:0x600000ca8320 UIView:0x7f988cf0e840.height == 80   (active)>",
    "<NSLayoutConstraint:0x600000cb1400 V:[UIView:0x7f988cf0e840]-(0)-|   (active, names: '|':UITableViewCellContentView:0x7f988cf0e420 )>",
    "<NSLayoutConstraint:0x600000cb14a0 V:|-(0)-[UIView:0x7f988cf0e840]   (active, names: '|':UITableViewCellContentView:0x7f988cf0e420 )>",
    "<NSLayoutConstraint:0x600000cbcdc0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f988cf0e420.height == 80.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000ca8320 UIView:0x7f988cf0e840.height == 80   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

我的自定义表格视图单元格

我的应用程序的外观

标签: iosswiftxcodetableviewios13

解决方案


推荐阅读