Указание минимальной высоты ячейки выдает предупреждения? - PullRequest
0 голосов
/ 16 ноября 2018

Я пытаюсь получить минимальную высоту ячейки для ячейки tableView.Это работает, но выдает миллиард предупреждений в консоль отладчика XCode.

Вопрос 1 : Почему?

Вопрос 2 : Какизбавиться от этих предупреждений при реализации минимальной высоты ячейки?

2018-11-16 19:18:13.080131+0100 Test[46195:5492055] [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:0x600003a91450 V:[UILabel:0x7fa4f2e05470'One ']-(0)-|   (active, names: '|':UITableViewCellContentView:0x7fa4f2e17f70 )>",
    "<NSLayoutConstraint:0x600003a91310 V:|-(0)-[UILabel:0x7fa4f2e05470'One ']   (active, names: '|':UITableViewCellContentView:0x7fa4f2e17f70 )>",
    "<NSLayoutConstraint:0x600003a911d0 UILabel:0x7fa4f2e05470'One '.height >= 44   (active)>",
    "<NSLayoutConstraint:0x600003a8dbd0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa4f2e17f70.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003a911d0 UILabel:0x7fa4f2e05470'One '.height >= 44   (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.
2018-11-16 19:18:13.081005+0100 Test[46195:5492055] [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:0x600003a953b0 V:[UILabel:0x7fa4f2c018d0'Three ']-(0)-|   (active, names: '|':UITableViewCellContentView:0x7fa4f2c06510 )>",
    "<NSLayoutConstraint:0x600003a95450 V:|-(0)-[UILabel:0x7fa4f2c018d0'Three ']   (active, names: '|':UITableViewCellContentView:0x7fa4f2c06510 )>",
    "<NSLayoutConstraint:0x600003a94be0 UILabel:0x7fa4f2c018d0'Three '.height >= 44   (active)>",
    "<NSLayoutConstraint:0x600003a95cc0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fa4f2c06510.height == 44   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003a94be0 UILabel:0x7fa4f2c018d0'Three '.height >= 44   (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.

Это можно просто воспроизвести с помощью этой очень простой настройки:

enter image description here

enter image description here

  • Новый UITableView с контроллером MyTableView
  • 1 прототип ячейки с классом MyTableViewCell
  • С ячейкой связан 1 ярлыкto MyTableViewCell.label

У меня есть следующие ограничения для метки:

enter image description here

Это нужно, чтобы убедиться, что у меня естьминимальная высота ячейки 44.

Контроллер вида:

import UIKit

class MyTableViewController: UITableViewController {
    let data: [String] = [
    "One ",
    "Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two Two ",
    "Three "
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableView.automaticDimension

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! MyTableViewCell

        cell.label.text = data[indexPath.row]

        return cell
    }

}

Ячейка:

import UIKit

class MyTableViewCell: UITableViewCell {
    @IBOutlet weak var label: UILabel!
}

Ответы [ 2 ]

0 голосов
/ 16 ноября 2018

Решение Петр Третьяков работает.

Другое решение, которое я нашел, было здесь .

Против вашей интуиции вам нужно ниже приоритет вашего> = ограничения с 1 от 1000 до 999. Тогда все работает, как и ожидалось, без предупреждений.

0 голосов
/ 16 ноября 2018

Проблема с ограничением высоты> = 44 UILabel в ячейке.Это происходит потому, что UITableView автоматически добавляет ограничение height = 44 в ячейку contentView.Только для ячейки с метками One и Three, поскольку для метки Two... для вычисления высоты строки используется размер метки.

Но contentView ячейки включает в себя не только вашу метку, но и разделитель с высотой по умолчанию 0,5Таким образом, ваша метка в высоте ячейки по умолчанию будет не 44, а 43,5, и поэтому ваши ограничения нарушаются.

Поэтому вам нужно установить ограничение высоты метки> = 43,5, и предупреждения исчезнут.

...