A file template that automates the setup of UICollectionView, including creating view controllers, collection view cells, and view models. This significantly reduces the repetitive work involved, streamlining the process and saving developers time.
If you're still working with UIKit and haven’t transitioned to SwiftUI, it’s important to recognize a key aspect: by not utilizing collection views, your app might be missing out on one of UIKit’s most powerful tools for building dynamic, highly flexible interfaces.
Apple’s Definition of CollectionView: A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually in a highly customizable way.
First you should download the collectionView-template and then copy it to this path ~/Library/Developer/Xcode/Templates/File Templates. It is vitally important to restart Xcode so as use the template.
To use this file template you have to have some code in your project.
** Fisrt **
Add the following code whereever you want.
protocol NibLoadable: AnyObject {
static var nib: UINib { get }
}
extension NibLoadable {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
}
extension NibLoadable where Self: UIView {
static func loadFromNib() -> Self {
guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
fatalError("The nib \(nib) expected its root view to be of type \(self)")
}
return view
}
}
extension NibLoadable where Self: UIViewController {
static var nibName: String {
return String(describing: self)
}
static var bundle: Bundle {
return Bundle(for: Self.self)
}
}
extension UIViewController: NibLoadable {}** Second **
protocol DefaultCollectionViewModelProtocol {
associatedtype Item: Hashable
var model: Item { get }
init(model: Item, delegate: AnyObject?)
func getDataModel() -> Item
}Now we can start by adding new file to your project and at this moment you can see there is a new template in the pop up.
By right clicking and selecting New File you should see this pop up.

For the sake of showing an example we select the View Controller & cell
Now you should see this pou up and you are supposed to choose one name for you UIViewController and one for Cell and its ViewModel.
The reason that you have to choose 2 names is because most of the time the ViewController has the bigger name. For instance it is more likely to have ArticlesViewController and choose Article for your Cell, CellViewModel and Model.

Now you will see these files have added to your project navigation:
At this moment you have your UIViewController and your disered CollectionViewCell and its ViewModel.
But pay attention: Next steps are required.
- Add a UICollectionView in your ViewController whereever you wish for.
- UnComment the collectionView outlet and asign it with the UICollectionView that you just added to your ViewController.

- Asign View outlet to the UIViewController

- Change Model, Cell, CellViewModel based on what you want.
- There are some comments in the ViewController that you have to change based on your situation.
and done.