Comments (3)
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.
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)
thenobj.loadNibContent()
to unarhive/load the NIB withobj
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 itsinit(coder:)
when the storyboard itself unarchives, then in thatinit(coder:)
method you can callself.loadNibContent()
to unarchive/load the content of the XIB withself
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.
Thank you for explanation!
from reusable.
Related Issues (20)
- getting error on using cellType(for indexPath:)
- Support for new API introduced on iOS 13
- MKMapView support
- Feature request - allow to use UITableViewCell with custom style
- Swift Package Manager: reusable deployment target warning HOT 3
- SPM xibs (Bundle.module) HOT 1
- Xcode 12.5 warnings HOT 2
- Swift tools version update too high, lower version build failed
- UITableViewHeaderFooterView - Warning with NibReusable
- Sure, go ahead!
- ## Summary
- Feature request - access to root xib view
- Support for reusable UITableViewHeaderFooterView subclasses?
- Privacy Manifest
- Swift 5 Support? HOT 2
- Undefined symbols when compiling for unit tests HOT 1
- Xcode 11 beta 4 compiler error HOT 1
- loadNibContent always loads nib named after base class HOT 14
- is there a way to override sceneStoryboard for StoryboardSceneBased HOT 2
- Swift Package Manager HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reusable.