0

I want to make a dynamic cell in my UITableView in case of image height. Everything works properly, but when I scroll my UITableView, debugger is yelling me about bad constraints in my cell, but cells look exactly what I want. I can't be calm when I see this debug info ;)

My CustomCell class looks like that:

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
    }
}
}

my cell in .xib looks like that:

enter image description here

TitleLabel doesn't have a height constraint. Leading, Top, Trailing, Bottom only.

and my ViewController is quite simple and looks like that:

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
}

}

How it looks like in simulator:

enter image description here

every thing look fine, but console gives me this kind of error:

enter image description here

I don't know why? I tried to use layoutIfNeeded(), updateConstraints() and setNeedsLayout() in didSet in CustomCell but is doesn't help. Any suggestions?

PiterPan
  • 1,760
  • 2
  • 22
  • 43
  • Have you set an estimated height for rows in your tableview or implemented the `estimatedHeightForRow(at:)` function? – Paulw11 Aug 15 '17 at 21:21
  • 1
    He has set `estimatedRowHeight` and used `rowHeight` of `UITableViewAutomaticDimension` which is all that's necessary. This sort of warning regarding the "Encapsulated Layout Height" can be solved by lowering one of your vertical constraints from 1000 to 999, which prevents the error, but the height is then correctly calculated. E.g. https://stackoverflow.com/q/25059443/1271826. Search SO for `UIView-Encapsulated-Layout-Height` and you'll see lots of discussions on this topic. – Rob Aug 15 '17 at 21:53

1 Answers1

0

I guess you're having some redundant constraints on your image. They could be height / bottom / top constraints. One should be removed

Hai Kieu
  • 1
  • 1
  • 1
    No, the fact that the error is related to `UIView-Encapsulated-Layout-Height` suggests that this is not the problem. It's a well-known, annoying "feature" of dynamically sizing rows. – Rob Aug 15 '17 at 22:08