How to update tableview cell height after updating image height constraint of image downloaded async?
How to trigger tableView cell relayout after image downloaded and constraints changed?
What's the best method to do this?
Already tried putting the code inside Dispatch main queue, but same bad results. I'm doing this in cellForRow method, also moved it to willDisplayCell. Again and again this problem...
Example of code using Kingfisher library for image caching:
if let imgLink = post.imageLink {
if let url = URL(string: imgLink) {
cell.postImage.kf.setImage(with: url, placeholder: UIImage(), options: nil, progressBlock: nil) { (image, error, cacheType, imageURL) in
if let image = image, cell.tag == indexPath.row {
cell.heightConstraint.constant = image.size.height * cell.frame.size.width / image.size.width
}
}
}
}
You may try to call these 2 lines to cause cell heights be recalculated after an image becomes available:
tableView.beginUpdates()
tableView.endUpdates()
See in the documentation https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates: "You can also use this method followed by the endUpdates() method to animate the change in the row heights without reloading the cell."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With