Swift TableView单元格比例高度

时间:2018-09-30 02:47:19

标签: ios swift constraints

所以我有一个漂亮的图表要添加到tableview单元格中。我正在尝试使该单元格始终是单元格宽度的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 个答案:

答案 0 :(得分:0)

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

A,B,C,D,E = [x[0] for x in [A,B,C,D,E]]

通常,您不应直接在weatherGraph.heightAnchor.constraint(equalTo: weatherGraph.widthAnchor, multiplier: 0.5).isActive = true (或cell.contentView本身)上设置任何宽度/高度约束,因为它是由cell内部管理的,因此约束冲突。 / p>