Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

objective c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...