UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView

孤者浪人 提交于 2019-11-27 18:22:25
Samuel Spencer

If you've already created your UICollectionView in Storyboard, connected your dataSource and delegate, and you have added all of the required methods:

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView (not a required method - refer to this comment)
  • cellForItemAtIndexPath

Then the registerClass / registerCell method isn't required. However, if you need to reuse a view, data, or cells then you should use those methods so that iOS can populate your UICollectionView as needed. This can also be done in your Storyboard by setting the Prototype Cell (the same principle as the registerClass method.

Also, if you're looking for a good explanation on what registerCell does and how to use it check out this link and scroll to the bottom section titled "Cell and View Reuse."

Steve

Agree with RazorSharp's answer and wanted to point out that that key phrase for me in the Techtopia link is:

If the cell class was written in code, the registration is performed using the registerClass: method of UICollectionView, otherwise use registerNib

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