13

I have a UITableViewController subclass with its prototype cells mocked up in the storyboard.

There's a fair amount of code in the cellForRowAtIndexPath delegate method that sets up the cells. Problem is I don't need most of it if the cell is just being dequeued from the reuse pool, because it's already been done when the cell was dequeued the first time. I can't do it in the storyboard because there are some properties I can only access programmatically.

Does the UITableViewController call an initializer in my UITableViewCell subclass when it takes a prototype cell from the storyboard? I tried (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier but that does not appear to be part of the process.

birarduh
  • 762
  • 8
  • 19

3 Answers3

13

When a UITableView instance calls for dequeueReusableTileWithIdentifier:, the cell is not reinitialized. Instead, in that call, the UITableViewCell that is dequeued will call -(void)prepareForReuse. This is because reinitializing the cell is costly, and if we can provide a much simpler method for preparing for its reuse (eh, eh, get it?) it saves a whole lot of CPU work.

Ergo, if you're using custom cells, override UITableViewCell prepareForReuse.

CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
11

I probably didn't phrase my question properly but I was looking for here was actually (void)awakeFromNib.

Gets called once when the cell is dequeued and not on reuse. Allows me to do some setup programmatically that it doesn't make sense to do in the storyboard.

birarduh
  • 762
  • 8
  • 19
  • 1
    (vodi)awakeFromNib works perfect for stuff you want to happen only once, like adding a gesture recognizer to a UILabel. – Eneko Alonso Oct 10 '12 at 17:22
0

Because you are loading (deserializing) the cell from the storyboard (nib-file) -[UITableViewCell initWithCoder:] method is called.

Artem
  • 373
  • 2
  • 6