Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does dequeueReusableCellWithIdentifier: call an initializer in my UITableViewCell subclass?

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.

like image 780
birarda Avatar asked Jun 15 '12 18:06

birarda


2 Answers

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.

like image 80
birarda Avatar answered Oct 17 '22 18:10

birarda


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.

like image 13
CrimsonDiego Avatar answered Oct 17 '22 18:10

CrimsonDiego