Generic UITableView & UICollectionView

Pratheesh Bennet
2 min readJun 21, 2021

Almost every iOS and tvOS app use UITableView/UICollectionView to display a collection of items. These list views basically give life to your entire app. Eg: Menu items in food ordering, movie booking, flight booking, Netflix, Pinterest, etc., all these apps use these views as much as intuitive as possible as their core UI/UX by making use of the native deque reusability for performance.

Real-world generic UIcollectionView in action

Here we are going to see from a developer’s point of how we can make the UITableView/UICollectionView generic, reusable, and making it SOLID.

The idea is to make a generic view controller that accepts a generic type of Cell and UIModel associated with the cell. By changing the type of cell and UIModel we can reuse the generic view controller containing the UITableView or UICollectionView across our application. A Generic class makes the passed type as concrete and performs the operations on the concrete type.

1. Generic UITableView

Design: For a generic reusable UITableView, the design is as below

Implementation: A generic view controller that accepts a generic type of Cell and UIModel associated with the cell and passes the same to the generic data source class.

The data source class creates an instance of the generic CellConfigurator class. The CellConfigurator abstracts the configuration of the UITableViewCell that conforms to the DynamicDataCell protocol. Based on the concrete type, the corresponding cell configuration is called whilst the actual cell configuration lies inside the corresponding UITableViewCell.

Source Code: Don’t worry I am not going to fill the post with a lot of code snippets. Please find the full source code here (iOS Swift)

2. Generic UICollectionView

Design: For a generic reusable UICollectionView, the design is as below

Implementation: The implementation is the same as generic UITableView.

Source Code: Please find the full source code here (tvOS Swift)

Please reach me at pratheesh_db@hotmail.com in case of any queries. Happy Coding…!

--

--

Pratheesh Bennet

Product owner Falcon Forms (www.falconforms.app). Digitize your paperwork using Falcon Forms. Build and configure your own forms in an instant.