Create UICollectionViewCell programmatically without nib or storyboard

爱⌒轻易说出口 提交于 2019-12-10 13:37:19

问题


Is there a way to create UICollectionViewCell without using existing cell identifier and existing nib file in cellForItemAtIndexPath method?


回答1:


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];


来源:https://stackoverflow.com/questions/23890131/create-uicollectionviewcell-programmatically-without-nib-or-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!