Swift tableview 单元格比例高度

Posted

技术标签:

【中文标题】Swift tableview 单元格比例高度【英文标题】:Swift tableview cell proportional height 【发布时间】:2018-09-30 02:47:18 【问题描述】:

所以我有一个漂亮的图表,我要添加到一个表格视图单元格中。我试图让这个单元格的高度始终为单元格宽度的 1/2。我目前有以下约束设置:

            let cell = tableView.dequeueReusableCell(withIdentifier: "weatherCell", for: indexPath)

            weatherGraph.translatesAutoresizingMaskIntoConstraints = false
            cell.translatesAutoresizingMaskIntoConstraints = false
            cell.contentView.addSubview(weatherGraph)

            weatherGraph.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor).isActive = true
            weatherGraph.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor).isActive = true
            weatherGraph.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
            weatherGraph.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
            cell.contentView.heightAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: 0.5).isActive = true
            cell.selectionStyle = .none

这样我可以在设备方向改变时支持调整单元格的大小。有了我所拥有的,我收到以下布局错误:

[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:0x60000134a210 UITableViewCellContentView:0x7fcd28866c90.height == 0.5*UITableViewCellContentView:0x7fcd28866c90.width   (active)>",
    "<NSLayoutConstraint:0x60000134b890 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fcd28866c90.height == 187.667   (active)>",
    "<NSLayoutConstraint:0x60000134bd40 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fcd28866c90.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000134b890 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fcd28866c90.height == 187.667   (active)>

在我的heightForRowAt 中,我为这个特定的单元格返回UITableView.automaticDimension

weatherGraph上添加设置高度限制的调试消息:

[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:0x600000bee030 H:|-(0)-[Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30](LTR)   (active, names: '|':UITableViewCellContentView:0x7fb5e072cc90 )>",
    "<NSLayoutConstraint:0x600000bec910 Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.right == UITableViewCellContentView:0x7fb5e072cc90.right   (active)>",
    "<NSLayoutConstraint:0x600000bec960 V:|-(0)-[Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30]   (active, names: '|':UITableViewCellContentView:0x7fb5e072cc90 )>",
    "<NSLayoutConstraint:0x600000beca00 Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.bottom == UITableViewCellContentView:0x7fb5e072cc90.bottom   (active)>",
    "<NSLayoutConstraint:0x600000beca50 Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.height == 0.5*Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.width   (active)>",
    "<NSLayoutConstraint:0x600000bee120 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fb5e072cc90.height == 187.667   (active)>",
    "<NSLayoutConstraint:0x600000bedfe0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fb5e072cc90.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000beca50 Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.height == 0.5*Digital_Heat_Sheets_Dev_2.HeatWeatherGraph:0x7fb5e0747a30.width   (active)>

我无法弄清楚将高度设置为 187.667 的位置以及为什么它会覆盖我的比率约束。

【问题讨论】:

【参考方案1】:

您是否尝试在天气图视图本身上设置此宽高比?:

weatherGraph.heightAnchor.constraint(equalTo: weatherGraph.widthAnchor, multiplier: 0.5).isActive = true

通常您不应直接对cell.contentView(或cell 本身)设置任何宽度/高度约束,因为它由UITableView 在内部管理,因此约束冲突。

【讨论】:

我还应该将 weatherGraph(左/右/上/下)限制在单元格的内容视图中吗? @Aaron 是的,您仍然必须限制这些,以便单元格的 contentView 知道如何动态拉伸到您的内容(天气图)大小。 我更新了我的问题以包括将比率约束移动到weatherGraph 而不是单元格时产生的错误。 很难说这里发生了什么。但是您是否尝试过只设置这些约束一次?将它们设置在cellForItem 中可能会由于多次调用而导致冲突。只是一个观察。 另外我认为你可以删除cell.translatesAutoresizingMaskIntoConstraints = false

以上是关于Swift tableview 单元格比例高度的主要内容,如果未能解决你的问题,请参考以下文章

swift中tableView的单元格高度扩展时更改标签文本

Swift:tableView 行的高度,其 tableView 单元格具有动态行数的嵌套 tableView

UITableView 单元格的动态高度问题(Swift)

根据内容动态调整tableview单元格的高度 - iOS Swift

Swift TableView 单元[关闭]

TableView里面的tableview cell swift 3