画像の高さの場合、UITableView
に動的セルを作成したいと思います。すべて正常に動作しますが、私がUITableView
をスクロールすると、デバッガはセルの悪い制約について私に叫んでいますが、セルは正確に私が望むものに見えます。UITableViewCellの制約が正しく更新されていません
class CustomCell: UITableViewCell {
//MARK: - Outlets
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
//MARK: - Variables
var dream: MyDream? {
didSet {
if let item = dream, let image = UIImage(data: item.photoData) {
scaleImageView(image: image)
photoImageView.image = image
titleLabel.text = item.title
}
}
}
func scaleImageView(image: UIImage) {
let imageWidth = image.size.width
let imageScale = image.size.height/image.size.width
if imageWidth > photoImageView.bounds.width {
heightConstraint.constant = photoImageView.bounds.size.width * imageScale
} else {
heightConstraint.constant = imageWidth * imageScale
}
}
}
.xib
の私の細胞がそのようになっています:
TitleLabel
doesn't have a height constraint. Leading, Top, Trailing, Bottom only.
マイCustomCell
クラスはそのように見える);私はこのデバッグ情報を見るとき、私は冷静にできません
と私のViewController
は非常に単純で、次のようになります。
class ViewController: UIViewController {
//MARK: - Outlets
@IBOutlet var tableView: UITableView!
//MARK: - Variables
lazy var dreams = [MyDream]()
override func viewDidLoad() {
super.viewDidLoad()
createModel() // I mock data here :)
setTableView()
}
func setTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
tableView.register(UINib(nibName: String(describing: CustomCell.self), bundle: nil), forCellReuseIdentifier: "cell")
}
func createModel() {
for i in 0..<12 {
if i < 6 {
if let image = UIImage(named: "\(i + 1)"), let imageData = UIImageJPEGRepresentation(image, 100) {
let dream = MyDream(photoData: imageData, title: "Image number \(i + 1)")
dreams.append(dream)
}
} else {
if let image = UIImage(named: "\(i - 5)"), let imageData = UIImageJPEGRepresentation(image, 100) {
let dream = MyDream(photoData: imageData, title: "Image number \(i + 1)")
dreams.append(dream)
}
}
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dreams.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.dream = dreams[indexPath.row]
cell.selectionStyle = .none
return cell
}
}
:
すべてのもの罰金に見えますが、コンソールは私にこの種のエラーを与える:
私はなぜ知りません?私はlayoutIfNeeded()
,updateConstraints()
とsetNeedsLayout()
をdidSet
にCustomCell
で使用しようとしましたが、助けになりません。助言がありますか?
テーブルビュー内の行の推定高さを設定したか、または 'estimatedHeightForRow(at:)'関数を実装しましたか? – Paulw11
彼は 'estimatedRowHeight'を設定し、' UITableViewAutomaticDimension'の 'rowHeight'を必要としました。 「カプセル化されたレイアウトの高さ」に関するこのような警告は、縦方向の制約の1つを1000から999に下げることで解決できますが、これによりエラーは防げますが、高さは正しく計算されます。例えば。 https://stackoverflow.com/q/25059443/1271826 SOに 'UIView-Encapsulated-Layout-Height'を検索すると、このトピックに関する多くの議論が見られます。 – Rob