【发布时间】:2019-08-06 07:11:17
【问题描述】:
这不是重复的问题,因为这个问题没有真正的解决方案
我正在尝试使用约束通过其内容实现UITableViewcell 动态高度,但收到布局警告:
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点 在调试器中捕获它。中的方法 UIView 上的 UIConstraintBasedLayoutDebugging 类别列于 也可能有帮助。 2019-03-15 12:27:52.085475+0400 表格单元动态高度[31984:1295380] [LayoutConstraints] 无法同时满足约束。 以下列表中的至少一个约束可能是一个 你不想要。试试这个:(1)查看每个约束并尝试 找出你没想到的; (2) 找到添加的代码 不需要的约束或约束并修复它。 ( "", "", "", "")
我检查了一些线程: Dynamic tableViewCell height
Dynamic Height Issue for UITableView Cells (Swift)
Swift 3 - Custom TableViewCell dynamic height - programatically
什么是正确的解决方案,我错过了什么?
视图控制器:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
lazy var tableView: UITableView = {
let table = UITableView()
table.backgroundColor = .white
table.translatesAutoresizingMaskIntoConstraints = false
table.register(TableViewCell.self, forCellReuseIdentifier: "cellId")
table.dataSource = self
table.delegate = self
return table
}()
let arr:[Int:UIColor] = [345: UIColor.random, 422: .random, 23: .random, 344: .random,200: .random,140: .random]
var pickerDataVisitLocation = [203: "Home", 204: "Hospital", 205: "Other"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
self.view.addSubview(tableView)
//
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
tableView.tableFooterView = UIView()
}
}
extension ViewController {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! TableViewCell
let value:UIColor = Array(arr)[indexPath.row].value
let key = Array(arr)[indexPath.row].key
cell.setupViews(he: CGFloat(key), color: value)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: 1.0)
}
}
TableViewCell:
import UIKit
class TableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupViews(he:CGFloat, color:UIColor) {
let v:UIView = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(v)
v.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
v.backgroundColor = color
v.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
v.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
v.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
v.heightAnchor.constraint(equalToConstant: he).isActive = true
#warning("here is constraint error conflict with bottomAnchor and heightAnchor, need correct solution")
}
}
【问题讨论】:
-
如果您已经知道每个项目的确切高度,为什么还要使用带约束的动态高度?
-
拳头删除
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } -
警告如此简单,设置底部锚点或高度锚点不要同时设置,如果您的高度大于等于,则无需设置底部约束
-
@Vinodh 不起作用,如果我只使用
bottomAnchor或heightAnchor你可以测试它
标签: ios swift uitableview nslayoutconstraint