简体   繁体   中英

Updating Constraints in UITableViewCell are not properly

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:

在此处输入图片说明

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:

在此处输入图片说明

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

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM