I saw this used in a WWDC video but only very briefly. They didn't go in to how to create the actual xib file.
I've got a UITableViewCell subclass called MyCustomCell. In this I have several properties UILabels, UIImageViews, etc… all set up as IBOutlets.
Now, in my xib file…
What do I set as the file's owner? Where do I reference my MyCustomCell class is this the file's owner?
Once I've set the file's owner how do I link it with the root view of the xib?
I've tried a few settings but I keep getting errors when using it.
Oh, the code I'm using to register it is…
self.cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"];
Thanks
Best Answer
Normally you don't have to bother about the File's owner in that case, because when the
tableView
instantiates the cell from the provided/associatedUINib
along with thereuseIdentifier
. It will load all the top-level objects of the nib, and use only the first top-level object that is of classUITableViewCell
(or maybe just the first top-level-object regardless of the class? but in general you only have yourUITableViewCell
in your XIB anyway — without counting theFile's Owner
and theFirst Responder
which are only "proxies").In fact, the
tableView
will try to dequeue a cell and if it doesn't find a reusable one, it will create a new one using theUINib
you provided. It will be something similar to this:(That's of course a simplified version just to show the idea, I don't know if it actually calls these exact lines, but it should be quite close)
So the
File's Owner
is not used in this particular case, and you only need to put a simple customUITableViewCell
as the only top-level-object of your XIB file next to the existingFile's Owner
anfFirst Responder
(that, again, are only "proxies" / "External Objects references" and won't be instantiated and won't be part of the top-level-objects returned byinstantiateWithOwner:options:
).If it still doesn't work:
reuseIdentifier
of yourUITableViewCell
in IB (in the Object Inspector pane on the right once you selected your cell in IB), and used the exact same value for thisreuseIdentifier
property in IB that the one you use in your code.