Question

Ok, I know how to load a custom cell from a xib, via this code:

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustpmCellView" owner:self options:nil];
cell = (CustomCell *)[nib objectAtIndex:0];

But can someone explain what does the first row do?

I feel really stupid typing that every time and not knowing how exactly it works.

Was it helpful?

Solution

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustpmCellView" owner:self options:nil];

loadNibNamed returns all views under your xib. So we are holding that in array. Say here all view under CustpmCellView will be fetched and saved in array nib.

cell = (CustomCell *)[nib objectAtIndex:0];

We are getting first view from array, as that is our desired view, and then we are casting and assigning to cell object.

We need to assign new view to each cell in UITableView, so for that purpose every time new cell is needed, and we do that using above code snippet.

EDIT

[NSBundle mainBundle] , is explained at What the meaning of [NSBundle mainBundle] in iPhone?

OTHER TIPS

According to Docs

loadNibNamed:owner:options:

Unarchives the contents of a nib file located in the receiver's bundle.

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options

Parameters

name The name of the nib file, which need not include the .nib extension.

owner

The object to assign as the nib’s File's Owner object.

options

A dictionary containing the options to use when opening the nib file. For a list of available keys for this dictionary, see “Nib File Loading Options.”

Return Value

An array containing the top-level objects in the nib file. The array does not contain references to the File’s Owner or any proxy objects; it contains only those objects that were instantiated when the nib file was unarchived. You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top