2

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!

DIF
  • 2,470
  • 6
  • 35
  • 49
Ostmeistro
  • 394
  • 2
  • 15
  • Is it possible that it just looks like my class is of my custom type, because I convert it when I dequeue, and the static string on my custom class works because that is accessible even without a real instance? I am just brainstorming :) I can post more code on Monday. – Ostmeistro Mar 02 '12 at 15:39

3 Answers3

18

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.

theVurt
  • 293
  • 2
  • 12
  • I cannot upvote you but this is seriously helpful, I couldn't find this information anywhere in the docs or on the net, you have probably helped hundreds future travelers :) – Ostmeistro May 03 '12 at 12:34
  • The seriously frustrating thing is, initWithStyle... init method is denoted as the "Designated initializer" according to UITableViewCell.h. It's a shame that Objective C doesn't have "proper" OO constructors like Java or C# etc. – Herr Grumps Feb 19 '14 at 11:05
1

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

Toastor
  • 8,980
  • 4
  • 50
  • 82
  • Sorry, no, I have the cells style defined in the storyboard file. To clarify, now I set the text in the cells by passing in my data object to the cell. So In the code for the cell I have a method, which is called and it works. The problem is I cannot override initWithFrame or initWithStyle or even init and put code there, because it never fires. All the cells are created internally and mysteriously by avoiding init. It is not really a problem, but I think it is quite wierd. – Ostmeistro Mar 06 '12 at 10:07
  • For me, this was the right answer. Also, it's much useful than just for the cell, but also it's useful for the custom views inside the cell. Such a custom view can just work from the storyboard if one overrides its awakeFromNib method. This is awesome, thanks! – naitoon May 14 '13 at 18:53
0

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;    
}
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • Isn't that one deprecated? Anyway, it did not work, same result as overriding init itself none of them are fired, but a const string is initialized and can be gotten so it is definatively an instance of the class that never has gone through any init. Thanks' – Ostmeistro Mar 02 '12 at 11:32