0

In application I am writing I decided to use MVVM patter. I am binding ViewModels with Views using RxSwift + RxCocoa. Now I am facing a problem I cannot find any solution myself.

I have a list of ViewModels which I want to display in UITableView. In tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) method I am dequeueing a cell and I am binding one of ViewModels from the list to this cell. ViewModel has imageUrl Variable and I use Kingfisher to download this image and put it into ImageView in that cell.

So here is my problem now: when download is completed I have to reload this row so Auto Layout will calculate its correct height. But when I use tableView.reloadRows(at: [indexPath], with: .none) the binding is recreated and it ends up with infinite loop (binding -> image download completion callback -> reload row -> binding -> ...)!

Any ideas how to workaround this?

Tiero
  • 107
  • 11

1 Answers1

0

You don't need to reload the row for autolayout to calculate the height (actually, never do that). Configure your tableview to have adaptative height and you're done.

You can find a good example of this on https://www.raywenderlich.com/129059/self-sizing-table-view-cells

thibaut noah
  • 1,474
  • 2
  • 11
  • 39
  • Hi. Sadly this is not true. When row with image is displayed and image is downloaded nothing happens. I have to scroll this row off the screen and scroll it in again - then image is correctly displayed. – Tiero Aug 30 '17 at 12:59
  • @Tiero it is true, i have a huge project with a collectionview displaying images for all items and we never reload the row. If the image doesn't display it means your code is incorrect. – thibaut noah Aug 30 '17 at 13:02
  • So how do you constrain your ImageView to make it work? – Tiero Aug 30 '17 at 13:16
  • You didn't design your cells? – thibaut noah Aug 30 '17 at 13:32
  • Simple example: ImageView inside UITableViewCell. From now iv = ImageView, con = ContentView of that cell. I constrain iv.leadin = con.leading, iv.trailing = con.trailing, iv.top = con.top, iv.bottom = con.bottom. So basicly ImageView covers whole cell. I want cell height to adapt to ImageView's height. When image is downloaded while cell is being displayed nothing happens. I have to scroll up/down and then down/up again for it to appear. – Tiero Aug 30 '17 at 13:43
  • And that is because your code is incorrect. Autolayout and adaptative height, read tutorials on that and you will get there. – thibaut noah Aug 30 '17 at 13:49