【问题标题】:How to increase the size of particular UITableViewCell?如何增加特定 UITableViewCell 的大小?
【发布时间】:2021-01-05 02:02:06
【问题描述】:

上面是我的 UITableView。我想在按下右(向下箭头)按钮时,我的特定 UITableViewCell 看起来像下图。

但问题是,当我按下右(向下箭头)按钮时,我收到如下警告。

2020-09-18 12:00:15.107030+0530 ET WIFI[1789:44926] [LayoutConstraints] 无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。 尝试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 ( "<0x600002c21950 uiview:0x7fa558d3b040.height="="><0x600002c21c20 v:><0x600002c21c70 v:><0x600002c22030 uitableviewcellcontentview:0x7fa558d386c0.height="=">

<0x600002c21950 uiview:0x7fa558d3b040.height="=">

标签: ios swift uitableview autolayout


【解决方案1】:

不幸的是,有几种可能性。

  1. 您的单元格内部存在约束问题。我假设这是一个情节提要,并且有一个绑定到viewHeight 并设置为 100 的约束。尝试将其设置为 200 并查看情节提要中是否出现错误。如果你这样做,请在此处修复它。如果您不这样做,请继续。

  2. 您不使用自动行高。检查是否在情节提要中将它们设置为自动或在您的代码中。如果在您的代码中为行方法实现高度,甚至在表格视图上设置行高,那么您必须在代码中解决此问题(在情节提要中更改它是不够的)。

  3. 问题在于优先级。这实际上很常见。在其中一个步骤中,您的系统可能无法解决约束,因此它开始抱怨。它通常稍后会解决它们,但看到这些警告很烦人。在单元格中,它通常可以通过降低情节提要中的约束优先级来解决。例如,我确信您可以将 viewHeight 优先级从 1000 降低到 900,而不会破坏任何逻辑。在大多数情况下,我实际上会降低固定到单元格底部的约束的优先级。

与问题没有直接关系,但请在您的视图控制器上使用weak,否则您将遇到保留周期和内存泄漏。应该是:

weak var viewController: HomeViewController? 

此外,在调整单元格大小时,我们通常不会在表格视图上调用重新加载。只需调用即可使其无效就足够了

self.viewHeight.constant = 200
self.viewController?.roomTableView?.beginUpdates()
self.viewController?.roomTableView?.endUpdates()

【讨论】:

  • 更改优先级解决方案帮助我消除了警告...非常感谢。
【解决方案2】:

当你增加视图的高度时,你需要增加 tableView Cell 的高度

struct TableData {
    var height: CGFloat?
    var color: UIColor?
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var tableData = [TableData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        for i in 0...10{
            self.tableData.append(TableData(height: 50, color: .red))
        }
        self.tableView.reloadData()
        // Do any additional setup after loading the view.
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.tableData[indexPath.row].height ?? 0
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tableData.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.tableData[indexPath.row].height == 100 {
            self.tableData[indexPath.row].color = .red
            self.tableData[indexPath.row].height = 50
            self.tableView.reloadRows(at: [indexPath], with: .automatic)
        }
        else {
            self.tableData[indexPath.row].color = .red
            self.tableData[indexPath.row].height = 100
            self.tableView.reloadRows(at: [indexPath], with: .automatic)
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell!.backgroundColor = self.tableData[indexPath.row].color
        return cell!
    }

}

didselect part will come in ur button click part

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2012-12-21
    • 2011-05-25
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多