使用没有Style的reuseIdentifier自定义UITableViewCell? [英] Custom UITableViewCell with reuseIdentifier without Style?

查看:110
本文介绍了使用没有Style的reuseIdentifier自定义UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的tableview中,我需要一堆(5-6种)不同的细胞。所有项目都具有相同的视觉布局,但内容明智(标签名称,图片和颜色),它们有很大不同。

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.

所以我有一个CustomUITableViewCell基类,在Interface Builder中使用这种通用设计设计。这个CustomUITableViewCell类服务器作为一堆单元子类的基类。
我使用工厂模式从工厂类中使用类方法生成这些子类。
这些子类没有xib。为什么他们会有共同的设计。

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.

现在问题是,对于每个子类我需要一个不同的重用标识符。所以,人们会认为让我们覆盖默认的初始化器每个子类,并在其中,调用另一个初始化程序, initWithStyle:reuseIdentifier:

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

问题是它需要要指定的样式。它抱怨说我不能把 nil 放在那里。但是我不需要Apple的任何风格,我显然有自己的风格,如果我想要一个股票风格,为什么我会做自定义设计。我只需要指定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.

如果它只是readonly属性,如何分配重用标识符,似乎提供它的唯一方法是通过初始化程序?

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

推荐答案

前段时间我遇到过类似的问题。我的解决方案是在实现文件中重新声明 reuseIdentifier 作为读写属性( UITableViewCell 子类)

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;

现在你可以分配 self.reuseIdentifier 了自定义单元格的init方法。

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

这篇关于使用没有Style的reuseIdentifier自定义UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆