iosuitableviewnibawakefromnibuinib

awakeFromNib not called when using instantiateWithOwner: method


I have a little problem and I need to understand how stuff works. I've searched within SO but I didn't find any valid solution.

My scenario is the following.

I have a UITableViewController that loads cells (within its table using) by means of UINib method instantiateWithOwner:.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
    if (!cell) {

        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    // other stuff here...

    return cell;
}

Cells are displayed correctly. Now, I need to customize each cell based on different criteria and I think the correct place to do it is in awakeFromNib method. Fr example, within CustomTableViewCell.m, I override awakeFromNib like the following:

- (void)awakeFromNib
{
    // my internal stuff here
}

But awakeFromNib is not called.

Could you explain what I'm doing wrong?

Thank you in advance.


Solution

  • Actually you need to customize in the code you showed 'other stuff here'. Since you recycle cells you need to insure the cell is configured correctly at each usage.

    EDIT: I made a test project, and verified awakeFromNib is in fact sent to the class. This leads me to believe the root problem is that in your nib with the cell, you have not set the class of the cell to your CustomClass name. Its the only possible explanation.

    EDIT2: So to reiterate:

    Something has to give here. What is your deployment target? In my test I used 5.1