I've lately seen some code sample that used instantiateWithOwner
to load the cells for a UITableView
this way:
cell = [nib instantiateWithOwner:self options:nil][0];
while it's cleaner and more compact than having a property outlet as a prototype cell, I wonder if it's guaranteed(aka documented) that the first top level object in the interface builder will always be the first in the array returned by instantiateWithOwner
. It may be the case at this point, but if not documented(undefined), it could change in the future, causing trouble for nibs that have more than one top level object and instantiate cells like this.
I can't find anywhere that documents the order of the array returned by instantiateWithOwner
specifically, but the documentation does talk about the order that nib-loading code calls awakeFromNib:
The order in which the nib-loading code calls the
awakeFromNib
methods of objects is not guaranteed.
My assumption is that this means the order of the array returned by instantiateWithOwner
is also not guaranteed. To guarantee order when pulling multiple objects out of a xib, I usually start by configuring each object's tag through interface builder with a unique (ascending) number. Then, your code would look something like this:
NSArray *cells = [[nib instantiateWithOwner:self options:nil] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
cell = cells[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With