Быстрая пропорциональная высота ячейки таблицы - PullRequest
0 голосов
/ 30 сентября 2018

Итак, у меня есть хороший график, который я добавляю в ячейку таблицы.Я пытаюсь сделать так, чтобы эта клетка всегда была на 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 голосов
/ 30 сентября 2018

Вы пытались установить это соотношение ширины и высоты на самом графике погоды?:

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

Обычно вы не должны устанавливать какие-либо ограничения ширины / высоты для cell.contentView (или * 1005).* непосредственно), так как он внутренне управляется UITableView, поэтому ограничения конфликтуют.

...