Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update tableview cell height after image downloaded and height constraint changed swift?

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   
                        }
                    }
                }
    }
like image 450
Daniel Z. Avatar asked Jan 28 '23 11:01

Daniel Z.


1 Answers

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."

like image 123
ppalancica Avatar answered Feb 13 '23 05:02

ppalancica