Bhavuk Jain
Bhavuk Jain

Reputation: 2187

TableView cell not resizing after changing imageView height constraint based on Image height downloaded using SDWebImage (AUTOLAYOUT)

I'm using autolayout.This is my code using it under cellForRowAtIndexPath:

cell.chatImage.sd_setImageWithURL(NSURL(string: newsFeed.postImage!), placeholderImage: UIImage(named: "default_profile"), completed: { (image, error, cacheType, imageURL) -> Void in

                let height = ((image?.size.height)! / (image?.size.width)!) * (tableView.bounds.width - 16)
                cell.chatImageHeight.constant = height
                self.view.layoutIfNeeded()
            })

The cell doesn't resize. It only resizes after scrolling. What should I do? Since I'm using autolayout, I'm not implementing heightForRowAtIndexPath. I've already mentioned UITableViewAutomaticDimension and estimatedRowHeight.

Upvotes: 1

Views: 697

Answers (1)

Mohamed Mostafa
Mohamed Mostafa

Reputation: 1057

I think you have to call self.view.setNeedsLayout() this will make the whole view to layout fixing the constraints

cell.chatImage.sd_setImageWithURL(NSURL(string: newsFeed.postImage!), placeholderImage: UIImage(named: "default_profile"), completed: { (image, error, cacheType, imageURL) -> Void in

                let height = ((image?.size.height)! / (image?.size.width)!) * (tableView.bounds.width - 16)
                cell.chatImageHeight.constant = height

                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()

            })

Upvotes: 1

Related Questions