以编程方式创建 UICollectionViewCell 无需笔尖或故事板 [英] Create UICollectionViewCell programmatically without nib or storyboard
问题描述
有没有办法在 cellForItemAtIndexPath
方法中不使用现有的单元标识符和现有的 nib 文件来创建 UICollectionViewCell
?
Is there a way to create UICollectionViewCell
without using existing cell identifier and existing nib file in cellForItemAtIndexPath
method?
推荐答案
CollectionViewCell 没有必要使用 Storyboard/Xib.您甚至可以以编程方式创建它.子类 UICollectionViewCell
并编写单元格的代码.然后在 CollectionView 控制器类中,您必须使用 collectionView
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.
在 cellForItemAtIndexPath 中使用正确的标识符出列
In cellForItemAtIndexPath use the proper identifier to dequeue
[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
这篇关于以编程方式创建 UICollectionViewCell 无需笔尖或故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!