15

I'm trying to use a custom subclass of UITableViewCell with the style set to UITableViewCellStyleSubtitle and use it with registerClass:forCellReuseIdentifier:. However, I'm not sure how to do this.

My thinking was to override the init method and call

self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myReuseIdentifier];

within it. However, I can't figure out which init method gets called (or if this is even the right way to do this.)

I've tried to intercept all of the following init methods, but none of them seem to be the correct one when dequeueing a cell:

  • init
  • initWithCoder
  • initWithFrame

I've confirmed that dequeueReusableCellWithIdentifier:forIndexPath: is returning a cell of the correct type so I know it's being created. I just don't know which init method is being called so I can't set the default style.

EXC_BAD_ACCESS
  • 346
  • 1
  • 4
  • 19
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

2 Answers2

36

I believe it calls initWithStyle:reuseIdentifier: for the table cell init.

From the apple docs

dequeueReusableCellWithIdentifier:

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

J

John
  • 2,640
  • 1
  • 16
  • 16
2

[[head-slap]] I'm an idiot! The method that I was trying to add in the body of the init call...

initWithStyle:reuseIdentifier:

...is the one I should be overriding!

Found it here...

Setting style of UITableViewCell when using iOS 6 UITableView dequeueReusableCellWithIdentifier:forIndexPath:

Voting to close my own question. Hopefully though, the brevity of this one will help others avoid reading the superfluous details in the other.

Community
  • 1
  • 1
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286