English 中文(简体)
作为子类的 Init 类分类
原标题:Init class as subclass

My question is kind of weird, lets say I have a class inheriting from UITableViewCell called GenericTableViewCell and some more classes inheriting from GenericTableViewCell. I want to be able to pass an argument to the GenericTableViewCell init method that will tell me which subclass of GenericTableViewCell should init this TableViewCell as. Heres what I thought of but I know that it will fail because it has a recursive loop in it.

@implementation GenericTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellIdentifier: (CellIdentifier *) identifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        if ([identifier class] == [FirstIdentifier class]){
            self = [[FirstTableViewCell alloc] initWithStyle:style reuseIdentifier:reuseIdentifier];
        }
        /// more else statements to check for other identifier cases
    }
    return self;
}

@end

是否有办法做到这一点? 或者我是否应该检查输入函数之外的身份识别码,然后由它来决定我应该申报哪个细胞?

最佳回答

是的, 您可以做到这一点。 这是您有时遇到的一种模式, 叫做类集。 如果您没有使用 ARC, 您必须释放自我的原始值, 以阻止内存泄漏 。

但是,我不会这样做。我会在 GenericTableViewCell 中创建一种工厂方法。

+(GenericTableViewCell*) cellWithStyle: (UITableViewCellStyle)style 
                       reuseIdentifier: (NSString *)reuseIdentifier 
                        cellIdentifier: (CellIdentifier *) identifier
{
    GenericTableViewCell* ret = nil;

    if ([identifier class] == [FirstIdentifier class])
    {
        ret = [[FirstTableViewCell alloc] initWithStyle:style reuseIdentifier:reuseIdentifier];
    }
    else 
    {
        // ....
    }
    return [ret autorelease];
}

您可以删除 if 语句, 方法是在单元格识别符中添加一个方法, 并在这样的子分类中覆盖该方法 :

// in CellIdentifier.m

-(id) classForCell
{
    return [GenericTableViewCell class];
} 

// in FirstIdentifier.m

-(id) classForCell
{
    return [FirstTableViewCell class];
}

然后你的工厂方法就变成

+(GenericTableViewCell*) cellWithStyle: (UITableViewCellStyle)style 
                       reuseIdentifier: (NSString *)reuseIdentifier 
                        cellIdentifier: (CellIdentifier *) identifier
{
    return [[[[identifier classForCell] alloc] initWithStyle:style 
                                             reuseIdentifier:reuseIdentifier] autorelease];
}
问题回答

您应该执行的东西被称为类组群图案。 在您的情况中, 您不应该在子类中而不是在子类初始化器中调用 init without Style: reuseIidation: : :

Generic TableViewCell 中:

- (id)initWithCustomIdentifier:(NSString *identifier) {
    Class cellClass = NSClassFromString(identifier);

    if (!cellClass) {
        cellClass = [MyStandardTableViewCell class];
    }
    self = [[cellClass alloc] init];

    return self;
}

MyStandard TableViewCell (或 Generic TableViewCell 的任何其他子类):

- (id)init {
    self = [super initWithStyle:someStyle reuseIdentifier:NSStringFromClass([self class])];

    if (!self) return nil;

    // do extra setup here
    return self;
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签