Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C中的类继承_Objective C - Fatal编程技术网

Objective c 目标C中的类继承

Objective c 目标C中的类继承,objective-c,Objective C,我有一个这样的父类: @interface SGBaseTableViewCell : UITableViewCell + (CGFloat)defaultHeight; ... @end SGBaseTableViewCell是我所有自定义UITablelViewCell @implementation SGBaseTableViewCell : UITableViewCell + (CGFloat)defaultHeight { static CGFloat default

我有一个这样的父类:

@interface SGBaseTableViewCell : UITableViewCell

+ (CGFloat)defaultHeight;
...

@end
SGBaseTableViewCell
是我所有自定义
UITablelViewCell

@implementation SGBaseTableViewCell : UITableViewCell

+ (CGFloat)defaultHeight {

    static CGFloat defaultHeight = 0.0;

     static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        SGBaseTableViewCell *cell = [[self class] newDefaultCell]; // newDefaultCell will just load the cell from a xib
        defaultHeight = cell.height;
    });
    return defaultHeight;
 }
@end
我希望每个自定义单元格将返回其高度。我的代码的问题是,它总是为每个单元格返回相同的高度(将返回第一个单元格的高度)

是否有一种解决方案,即每个单元格将返回其高度,而不在子类中重写
defaultHeight
方法

PS:我知道我可以覆盖每个子类中的
defaulthight
方法以返回适当的高度,但是我想知道我是否可以在基类中突出它

Tnaks

我的最终解决方案(在其他论坛上提出):


我从RayWenderlich那里找到了这篇文章:


这似乎是一个很好的替代解决方案,您不必覆盖每个子类的任何方法。

当您指“每个单元格的高度相同”时,您指的是单元格的每个子类或tableView中的每个单元格(您可能有多行标签的单元格)?如果使用
UITableViewCell*cell=[[self class]新细胞]
?@Tanguy of corse你可以,在一个类方法中self是类,然后我就不知道了:/遗憾的是,我总是在cell子类中重写我的
+(CGFloat)height
。@Tanguy我已经回答了我的问题,如果你感兴趣的话:)很好的解决方案。谢谢你的链接。
+ (CGFloat)defaultHeight
{
    static dispatch_once_t onceToken;
    static NSMutableDictionary *heights;

    dispatch_once(&onceToken, ^{ heights = [NSMutableDictionary new]; });

    @synchronized(self)
    {
        NSString *key = NSStringFromClass(self);
        NSNumber *h = heights[key];
        if (h) return [h floatValue];
        SGBaseTableViewCell *cell = [self newDefaultCell];
        [heights setValue:@(cell.height) forKey:key];
        return cell.height;
    }
}