Reload UITableView row after downloading image (using MVVM)

1k views Asked by At

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?

1

There are 1 answers

6
thibaut noah On

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