如何在tableView其他控制器中单击单元格更改高度?

问题描述 投票:0回答:1
2023-09-20 09:57:39.883571+0700 Dokobit[5279:68007] [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:0x60000370b890 UITableView:0x7f9d5082ba00.height == 106   (active)>",
    "<NSLayoutConstraint:0x60000370a4e0 UITableView:0x7f9d5082ba00.height == 159   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000370a4e0 UITableView:0x7f9d5082ba00.height == 159   (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.

我想单击警报控制器中的单元格当前使高度表视图其他控制器更改

func getValueCell(withValue value: String) {
    coutry = value
    btnAllCountries.setImage(UIImage(named: value), for: .normal)
    if (value == "Estonia") {
        DispatchQueue.main.async {
            self.heightTableView(height: 159)
        }
    } else if (value == "lithuania") {
        DispatchQueue.main.async {
            self.heightTableView(height: 106)
        }
    }
}

func heightTableView(height: Float ) {
    var tableViewHeightConstraint = tableView.heightAnchor.constraint(equalToConstant: CGFloat(height))
    tableViewHeightConstraint.isActive = true
    print("OK")
}
swift uikit constraints
1个回答
0
投票

您遇到的警告消息表明与 UITableView 高度相关的两个约束之间存在冲突。具体来说,您试图同时在同一个 UITableView 上激活两个不同的高度约束,这会导致冲突。要解决此问题,您应该在添加新的高度限制之前停用或删除以前的高度限制。

这是 heightTableView 函数的更新版本,它在添加新的高度约束之前删除了现有的高度约束:

func heightTableView(height: Float) {
    // First, deactivate any existing height constraint
    tableViewHeightConstraint?.isActive = false
    
    // Create a new height constraint based on the provided height
    let newConstraint = tableView.heightAnchor.constraint(equalToConstant: CGFloat(height))
    newConstraint.isActive = true
    
    // Assign the new constraint to the tableViewHeightConstraint property
    tableViewHeightConstraint = newConstraint
    
    // Tell the view to update its layout
    view.layoutIfNeeded()
}
© www.soinside.com 2019 - 2024. All rights reserved.