无编程或故事板以编程方式创建UICollectionViewCell

Ale*_*rNS 6 ios uicollectionview uicollectionviewcell

有没有办法在UICollectionViewCell不使用现有单元标识符和cellForItemAtIndexPath方法中的现有nib文件的情况下创建?

Ani*_*ese 12

没有必要将Storyboard/Xib用于CollectionViewCell.您甚至可以以编程方式创建它.子类UICollectionViewCell并编写单元格的代码.然后在CollectionView控制器类中,您必须使用collectionView注册自定义单元类

[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID]; 
Run Code Online (Sandbox Code Playgroud)

如果使用不同类型的单元格,则可以使用不同的单元格类.重用标识符应该是唯一的.

cellForItemAtIndexPath中使用正确的标识符出列

[collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

  • 快速:例如:`self.myCollectionView.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:“ identifier”)` (2认同)