Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...