Create UICollectionViewCell programmatically witho

2019-06-19 03:06发布

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

1条回答
再贱就再见
2楼-- · 2019-06-19 03:27

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];
查看更多
登录 后发表回答