DenTelezhkin/DTCollectionViewManager

registerCellClass with no nibs

vendruscolo opened this issue · 4 comments

Currently I'm not using nibs nor storyboard, thus I just call registerCellClass:. When the CollectionView tries to dequeue a cell it raises an exception saying that no cell class has been registered.
The fix is easy, I just need to call collectionView?.registerClass(CellClass.self, forCellWithReuseIdentifier: "<reuse identifier that would be generated with reflection>") from my view controller.
This kind of stuff could go in the library itself, in my opinion. Also because if one day you change identifier, my code breaks.

What do you think?
Before I open a PR, I'd like to know if it would be enough to add the code above to an else clause in the registerCellClass: method or if there's no else clause because that's needed for storyboards compatibility: if no nib exists it means that the view is in the storyboard?

If that's the case, I propose to handle the pure (code only) case in the method registerCellClass: and add a new method registerCellClassWithNoStoryboardNorNib: that has the current implementation (suggestions for method names are appreciated!!).

(I'll obviously update the cousin registerSupplementaryClass: forKind: method)

Creating cells from code was not supported, because there's no way to actually differentiate between cell, registered by storyboard, and cell, that does not have any interface document attached. I thought it was fine, because using cells from code is really uncommon these days.

However, it seems there's still need to create cells from code - this is third or fourth feature request about this, so i'll consider implementing this in the next release. Although i'm certain now, that this would require separate methods, that will require user to explicitly say - "Yes, i know this cell does not have any interface document", something like "registerCodeCreatedCell". Naming could be improved, obviously =)

At the moment I implemented what I suggested using the [else clause](https://github.com/vendruscolo/DTCollectionViewManager/commit/f07b161e344fde97f82e82029e3d47d39ce71a9f <https://github.com/vendruscolo/DTCollectionViewManager/commit/f07b161e344fde97f82e82029e3d47d39ce71a9f); as I said, I thought the else clause was missing to support storyboards, so that code won’t work for any project (in my project, there are no storyboard so it’s fine).
registerCodeOnlyCell: or the like could be ok, and it’s also backwards compatible with the current version.

I've released DTCollectionViewManager 4.2.0, that contains method registerNiblessCellClass and registerNiblessSupplementaryView among other things.

Enjoy 🍻

Fantastic news, can’t wait to stop using my fork! (I’m also happy that #12 was fixed by removing code).

Thank you