Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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;    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top