我正在使用 UITableViewController
并且正在加载自定义的 UITableViewCell
。单元格中的所有元素和约束都以编程方式配置。 TableView 看起来像这样
除删除单元格外,一切正常。我将 tableView 配置为在删除时淡化单元格。但是,该单元格稍微移动了一下,然后向左移动而不是褪色,我在控制台中收到自动布局错误消息:“无法同时满足约束条件”
"<NSLayoutConstraint:0x600002551a40 UIView:0x7fccbd5286f0.height == 100 (active)>",
"<NSLayoutConstraint:0x6000025505a0 V:|-(16)-[UIView:0x7fccbd5286f0] (active, names: '|':UITableViewCellContentView:0x7fccbd541b10 )>",
"<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>",
"<NSLayoutConstraint:0x6000025605a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fccbd541b10.height == 1.19209e-07 (active)>"
及以下
"<NSLayoutConstraint:0x6000025505a0 V:|-(16)-[UIView:0x7fccbd5286f0] (active, names: '|':UITableViewCellContentView:0x7fccbd541b10 )>",
"<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>",
"<NSLayoutConstraint:0x6000025605a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fccbd541b10.height == 1.19209e-07 (active)>"
下面的“将尝试通过打破约束来恢复”
<NSLayoutConstraint:0x600002551a40 UIView:0x7fccbd5286f0.height == 100 (active)>
<NSLayoutConstraint:0x600002550730 UIView:0x7fccbd5286f0.bottom == UITableViewCellContentView:0x7fccbd541b10.bottom (active)>
I broke the console output into parts because I couldn't paste it all together for some reason.
基本上,我在单元格的 contentView
内有一个名为 cellView
的 UIView
(白色圆角区域)。您看到的所有其他元素都在 cellView
中。我在 cellView
和 contentView
cellView.translatesAutoresizingMaskIntoConstraints = false
cellView.heightAnchor.constraint(equalToConstant: 100).isActive = true
cellView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16.0).isActive = true
cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
cellView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16.0).isActive = true
cellView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16.0).isActive = true
当我在删除单元格之前尝试停用单元格及其 subview 中的所有约束时,它起作用了。但是,当新细胞被重复使用时,它们没有激活限制。
我应该如何处理这种情况?
最佳答案
你需要降低底部约束的优先级
let con = cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
con.priority = UILayoutPriority(999)
con.isActive = true
关于ios - 当从 UITableView 中删除以编程方式自定义的 UITableViewCell 时,约束会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52803937/