Ostmeistro
Ostmeistro

Reputation: 394

Prototype cells with custom class does not fire init?

I am pretty new to objective-c and have a question regarding prototype cells.

I have a tableview with custom cells, and it works nicely.

Now I have also in my custom cell class overridden -(id)init and -(id)initWithStyle: reuseIdentifier:

If I do a class check on the cell, it is clearly of my custom class, but neither init method is ever called.

So it creates them for me, but somehow avoids firing -(id)init which seems wierd to me.

I guess I could init them on my own but it seems really wierd that they can exist without having been created?

Thank you!

Upvotes: 2

Views: 1795

Answers (3)

theVurt
theVurt

Reputation: 293

If its a prototype cell from a story board. - (id)initWithCoder: gets called. So you need to override:

- (id)initWithCoder:(NSCoder *)coder
{
  self = [super initWithCoder:coder];
  if (self) {
       //custom stuff here
  }

  return self;
}

This is true for anything awakened from a storyboard.

Upvotes: 18

Toastor
Toastor

Reputation: 8990

Did you load these cells from a xib? If so, try using awakeFromNib instead.

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

The method you should use for init the custom cell is:

   -(id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier{

    if(self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]){

    }
    return self;    
}

Upvotes: 0

Related Questions