awakeFromNib not called when using instantiateWithOwner: method

2.9k Views Asked by At

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.

2

There are 2 best solutions below

2
On BEST ANSWER

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:

  • in CustomTableViewCell.xib, the class of the object is set to 'CustomTableViewCell'

  • add some log messages in CustomTableViewCell.m - in the initWithCoder: if you have it and also in awakeFromNib (make sure of spelling!!!)

  • when you instantiate here 'cell = [topLevelItems objectAtIndex:0];', add a log before NSLog(@"Going to instantiate"), the afterwards a log:

    NSLog(@"Instantiate a %@ cell", NSStringFromClass([cell class]);
    

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

0
On

I just had the same problem with awakeFromNib not called when using instantiateWithOwner. However, I changed back to use [[NSBundle mainBundle] loadNibNamed:name owner:self options:nil] and it didn't fix the issue.

Then, adding a test selector to my custom view implementation and of course, the app crashed on unrecognized selector.

So after a little gnashing of teeth and hair pulling, it turned out to be quite a basic error.

In the Xib associated with the custom view I had forgotten to enter the name in the identity inspector in Interface Builder. Consequently, the runtime thought my class to be an instance of UIView rather than MyCustomView. So the awakeFromNib breakpoint never triggered.

Bottom line: if you are having this problem using custom view classes with an associated Xib, check that you have set the class name correctly:

enter image description here