1

I have a UITableViewCell that has a UIImageView. Users can post images and they'll display in the UITableView (think similar to social network/media apps).

The problem I have is that after I create my post and I am back at the main UITableView, the cell is not resizing accordingly. But after I scroll and the cell goes off screen it updates to the correct size.

Because I am cropping the images and they can be portrait or landscape I am calculating the ratio of the image, unless they exceed a certain size then I set a constraint constant to a set size.

Here's some code to see what I am trying to do:

override func viewDidLoad() {
    self.myTableView.rowHeight = UITableView.automaticDimension
    self.myTableView.estimatedRowHeight = 500

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.myTableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   cell.myImageView.sd_setImage(with: imageRef, placeholderImage: nil) { (downloadedImage, error, cache, url) in
       if let image = downloadedImage as? UIImage {
           let ratio = image.size.width / image.size.height
           var newHeight = cell.myImageView.frame.width / ratio

            if newHeight >= tableView.frame.width {
                newHeight = tableView.frame.width
            } else {
                newHeight = cell.myImageView.frame.width / ratio
            }

            cell.myImageHeightConstraint.constant = newHeight

            if let indexPath = tableView.indexPath(for: cell) {
                tableView.reloadRows(at: [indexPath], with: .none)
            }
        }
    }
}

Update:

I feel like I'm getting closer but still having some issues. Above I have modified the code. So right now when I make my new post and the table reloads, the cell resizes with a slight animation. That's fine. Preferably I'd like the user NOT to see the cell animate to the correct size. The next issue is when I scroll down some of the cells may need to resize and it causes some jumpiness.

Luke Irvin
  • 1,179
  • 1
  • 20
  • 39
  • try to add `cell.layoutIfNeeded()` after computing cell's imageView height constraint – Olympiloutre May 17 '19 at 02:24
  • @Olympiloutre that didn't work – Luke Irvin May 17 '19 at 15:31
  • Just looked again at my code on which I do something similar. 1- In the .xib I set the constraint height priority to 999 (not 1000). 2- actually I call `cell.layoutIfNeeded()` *before* setting the height constraint. If non of these works, I suggest you to look at the `invalidateLayout` property. My guess is that it may come from the reusability of your cells, that needs to be cleaned/reset before reused. – Olympiloutre May 20 '19 at 05:15
  • Hint: if the problem does not happen for the very first cell to be displayed ( i.e. a cell that just got created, not one that has already been recycled/reused ) it should come from the reusability – Olympiloutre May 20 '19 at 05:16
  • @Olympiloutre Still no luck on this. I've updated to 999 & moved .layoutIfNeeded(). First cell (newly created cell) still comes back incorrect until I scroll it off screen. – Luke Irvin May 20 '19 at 20:17
  • Maybe it worth the try to take this all from the beginning. The issue may be hidden in the storyboard as well. Here is a perfect post that may help you to sort things out: https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Olympiloutre May 21 '19 at 06:15
  • @Olympiloutre It's looking like the cell frame is not resetting properly. So let's say I post a larger image and it resizes to 350 and then the next image posted is a thinner landscape. It's still setting the cell frame to match the previous image vs. reset and see what the new frame size should be. – Luke Irvin May 24 '19 at 12:25

0 Answers0