Generic table view controller
A reusing approach for table view controller.
Getting started
Hey folks! Today we are going to talk about generic things, take a look at the image above. What do you see? A simple iOS application navigation flow that presents some screens. What do they have in common? At first glance, we can observe four screens built on TableViewController, right? Do you agree? Probably the most intuitive way would be building five view controllers to accomplish this app, but I would like to share an approach that we are going to use just two. One for a generic table view controller used in all list screens and another to display a not found screen, this second view controller was built only to prove a concept that you can integrate another kind of view controllers with the concept that we are going to show here. Get the complete code on GitHub at the final of the article.
What does change?
If you take a look, indeed you are going to notice that the cells are the things that change in these table view screens. So, we can reuse the table view as a generic component and customize any cell we want.
Architecture
Generic table view controller
To achieve this goal using storyboard I think is very hard, I would not say impossible, but I spent much time, and I could not figure out an elegant solution, mostly because of the generic type we must inform into generic view controller and the way the storyboard instantiates does not favor that. So it is a complicated subject, and of course, there are pros and cons, if you want to read more about it I wrote a post talking about this subject on the link below:
So, to achieve this goal I used a programmatically view controller, take a look below:
The code above has all we need to perform a table view controller presentation, and it has (cellForAt) method, numberOfRowsInSection and didSelectRowAt methods implemented. Notice the generic type inferred by the constructor, this generic type has a constraint, and it must conform the protocol DescriptiveProtocol to provide a way to bind UITableViewCell for each item on (cellForAt) method. So, in this way, each element from data source must inform which cell type must be returned by reusable dequeue method from the table view. Here is the trick, the generic table view does not know anything about the cells, then it can be used for all of them.
The descriptor
The descriptor object is responsible for holding the cell type, reuse identifier and the closure that must be called to configure cell.
The model
As you can see, this struct conforms DescriptiveProtocol, and it has a property called descriptor, as mentioned above it has the responsibility to bind it into UIViewTableCell specialization, at this moment the cell type is informed when table view cellForAt method is called.
Conclusion and the souce code
The remaining code is self-explanatory, I think it is not a silver bullet, but can help someone in some specific context, feel free to comment if you have something to say about it. Bye and see you next time.