objective-cnsbundleloadnibnamed

Objective-C / Load cell from xib / Need explanation


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.


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?