Cells and ViewModels

With MVVM, each cell will have its own ViewModel from where it will get the necessary data it needs.

That's pretty sweet, until you have to dequeue a cell:

So, what's the identifier?

The ViewModel shouldn't be aware of the View.

And the reason why is: if at any point the View is replaced, the ViewModel should be able to exist without any change whatsoever. But on the other hand the View creation (UITableViewCell/UICollectionViewCell) is based on the ViewModel, so what now?

Something we have been using is:

Which will make the table:cellForRowAtIndexPath: pretty straightforward:

So depending on the ViewModel you will have a corresponding Cell1.

This approaches separates the ViewModel's core from the View via an extension. I am still looking for other approaches for this problem, but right now it has been working quite well.

  1. I might try this approach adapted to this use case.