Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to dequeue NibOwnerLoadable? #46

Closed
xiaoxinghu opened this issue Jun 12, 2017 · 3 comments
Closed

Is it possible to dequeue NibOwnerLoadable? #46

xiaoxinghu opened this issue Jun 12, 2017 · 3 comments

Comments

@xiaoxinghu
Copy link

If not, what is the recommended way to use NibOwnerLoadable on a cell(table/collection) class.

@AliSoftware
Copy link
Owner

NibOwnerLoadable is for when you design a XIB with your class being set on the File's Owner.

The way UITableView and UICollectionView work in CocoaTouch, as designed by Apple, impose you that all your custom cells designed in dedicated XIBs have an empty File's Owner and have their cell's contentView as the root of the XIB. So this configuration corresponds to the NibLoadable protocol, not the NibOwnerLoadable protocol.

NibLoadable

  • You don't have a File's Owner in your XIB
  • The view you intend to load is at the root of your XIB
  • When Interface Builder, or your UITableView or UICollectionView, will load the NIB file, it will unarchive the NIB and instantiate the root view and all its subview. That's how UITableView works, you don't have a choice for that, that's how Apple implemented the dequeueReusableCell(…) method for cells registered with a XIB/UINib.

So it's suitable for UITableViewCells & UICollectionViewCells, or for custom views you plan to create by code

NibOwnerLoadable

  • That's for when you set your File's Owner of your XIB to your custom class
  • By definition, in a XIB, a File's Owner is an instance that is supposed to be already created by yourself and that you pass to the UINib.instanciate(owner:options:) when unarchiving the XIB. So the way it's supposed to work in this configuration, given how XIB work, is that you create an instance of your class, then you unarchive the XIB by giving it that newly created instance as parameter to take the place of the File's Owner.

So it's only suitable for views that you either:

  • create by code (let obj = MyClass(frame: someFrame) then obj.loadNibContent() to unarhive/load the NIB with obj as its File's Owner)
  • or that you will add to another storyboard or XIB (because that other storyboard will then create your instance of MyClass by calling its init(coder:) when the storyboard itself unarchives, then in that init(coder:) method you can call self.loadNibContent() to unarchive/load the content of the XIB with self as File's Owner)

But in both case with NibOwnerLoadable you create an instance of your class (by code or because it's in another storyboard or XIB) then load the XIB, which is suitable for custom views / widgets that you plan to reuse in multiple places in your app, but not for UITableViewCell / UICollectionViewCell because that's not how Apple's dequeueReusableCell method works (it expects the class to be part of the NIB's root view and be instantiated when the NIB is unarchived, not already existing and be passed by reference as File's Owner)

@xiaoxinghu
Copy link
Author

Thanks for the crystal clear explanation. I think this writeup should be put in somewhere in the documents, for teaching people how it works behind the scenes. It is super useful (at least for rookie like me).

@upeugene
Copy link

upeugene commented May 7, 2018

Thank you for explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants