ios - 当从 UITableView 中删除以编程方式自定义的 UITableViewCell 时,约束会中断

标签 ios swift uitableview autolayout

我正在使用 UITableViewController 并且正在加载自定义的 UITableViewCell。单元格中的所有元素和约束都以编程方式配置。 TableView 看起来像这样

enter image description here 除删除单元格外,一切正常。我将 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 内有一个名为 cellViewUIView(白色圆角区域)。您看到的所有其他元素都在 cellView 中。我在 cellViewcontentView

之间应用了以下约束
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/

相关文章:

Swift indexPath 数组计算

swift - 如何在swift中正确使用CFStringGetCString?

ios - UITapGestureRecognizer 根本不工作

ios - 使用自定义 UITableView subview

ios - 如何在 swift 3 中过滤 UISearchBar 中的数组模型

ios - 根据 Swift iOS 中的大小对文档目录中的文件和文件夹进行排序

android - 在 react-native-map 中切换 maptype

iphone - 如何检测用户是否拒绝使用我的默认位置?

ios - 条件和 UIButton

ios - 如何确定当前在 SwiftUI 中是否选择了 View