Nowadays, we have a problem in iOS. Instead of MVC design pattern, which stands for Model-View-Controller, we have a disease called Massive-View-Controller.
As a good “refactoring skill”, we quickly adopted the MVVM (Model-View-ViewModel) design pattern. It is really good, no question about it, but let’s see one of the most common errors inside iOS – working with cells , UITableViewCells and UICollectionViewCells precisely, and the reason why we found ourselves infected by that “massive” virus.
Let’s say we want to present users inside our collection view:
We define our collection view cell like this:
Inside our view controller, which has our collection view, we will have:
And there, we have a problem. Three lines of code, instead of just one. Imagine that we add descriptionLabel and dateLabel. Two more lines of code added. So five lines instead of one.
The one would say why? Why is that a problem? It’s not a big deal, right?
Let’s think about it: I am a view controller and I have a collection view. Of course, I know that my collection view has some cells – but hey I don’t care what’s going to be presented inside those cells. Why would I care? Configuring collection view cells is not part of my job at all! Exactly, good decomposition is a must!
Let’s see how we can fix this:
First, why we declared our outlets to be private – well, no one should know about them actually. Anyone who is using this cell should know that it only has to provide a user and cell with configure itself accordingly.
Let’s see our “infected” view controller:
What do you think? At some point, we update our cell with some new labels, buttons, image views but the view controller would say: “Hey cell, I don’t care, here is your user object, configure yourself however you want.” And so, step-by-step, our code in view controllers will eventually get smaller – you know the one “less code, less bugs”.
Also, there are more examples where our view controller shouldn’t be involved at all and I will cover those of course. That is why MVVM is so powerful design pattern in the first place.