Earl Grey
Earl Grey

Reputation: 7466

Custom UITableViewCell with reuseIdentifier without Style?

In my tableview, I need a bunch (5-6 types) of different cells. All have the same visual layout for items, but content wise (label names, pictures and colors), they differ a lot.

So I have a CustomUITableViewCell base class, designed in Interface Builder with this common design. This CustomUITableViewCell class server as a base class for bunch of cell subclasses. I generate these subclasses with a class method from a factory class using factory pattern. These subclasses do not have xibs. Why would they, the have common design.

Now the problem is, for each subclass I need a different reuse identifier.So, one would think lets override the default initializer for each subclass, and in it, call another initializer, the initWithStyle:reuseIdentifier:.

The problem is it requires the style to specify. I can't put nil there, it complains. But I do not need any style from Apple, I have obviously my own style, why would I do the custom design if I wanted to have a stock style. I only need to specify the reuseIdentifier.

How to assign reuse identifier if it's readonly property and it seems that the only way to provide it is through the initializer?

Upvotes: 3

Views: 4271

Answers (1)

Martin R
Martin R

Reputation: 539935

I had a similar problem some time ago. My solution was to re-declare reuseIdentifier as read-write property in the implementation file (of the UITableViewCell subclass)

@interface MyCustomCell ()
@property(nonatomic, readwrite, copy) NSString *reuseIdentifier;
@end

and to synthesize the property with a different instance variable:

@implementation MyCustomCell
@synthesize reuseIdentifier = _myCustomCellReuseIdentifier;

Now you can assign self.reuseIdentifier in the init method of your custom cell.

At least it worked in my case, perhaps you can use it ...

Upvotes: 9

Related Questions