GithubHelp home page GithubHelp logo

Comments (3)

xiaoxinghu avatar xiaoxinghu commented on July 20, 2024 4

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).

from reusable.

AliSoftware avatar AliSoftware commented on July 20, 2024 2

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)

from reusable.

upeugene avatar upeugene commented on July 20, 2024

Thank you for explanation!

from reusable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.