Create UICollectionViewCell programmatically without nib or storyboard
10,766
Its not necessary to use Storyboard/Xib for the CollectionViewCell. You can even create it programmatically. Subclass UICollectionViewCell
and write up the code for the cell. Then in CollectionView controller class you have to register you custom cell class with the collectionView
[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
You can have different cell class if you are using different type cells. Reuse identifier should be unique.
In cellForItemAtIndexPath use the proper identifier to dequeue
[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
Author by
AleksandarNS
Updated on June 19, 2022Comments
-
AleksandarNS almost 2 years
Is there a way to create
UICollectionViewCell
without using existing cell identifier and existing nib file incellForItemAtIndexPath
method? -
PashaN almost 8 yearsin swift: ex:
self.myCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "identifier")