无论我做什么,这个变量都拒绝改变价值

时间:2011-11-15 10:02:14

标签: iphone objective-c ipad

应该很简单。相反,它在最后一个小时左右让我感到困惑。


标题文件:

CellBackgroundView是一个派生自UIView的自定义类。

@interface CustomCell : UITableViewCell {

    CellBackgroundView *customBackgroundView;

}

@property (strong) CellBackgroundView *customBackgroundView;


无法按预期运行的代码:

我想做的就是为customBackgroundView分配一些东西。

    customBackgroundView = [[CellBackgroundView alloc] initWithFrame:CGRectZero];
    [self addSubview:customBackgroundView];

-

什么都没发生。在调试器中,我可以看到customBackgroundView是0x0。以下也不起作用,虽然“背景”确实被分配了一个内存位置。 customBackgroundView仍然是0x0。

    CellBackgroundView *background = [[CellBackgroundView alloc] initWithFrame:CGRectZero];
    customBackgroundView = background.
    [self addSubview:customBackgroundView];


发生了什么事?为什么这不起作用? 我打开了ARC。


编辑:

CellBackgroundView的基础是:

- (BOOL) isOpaque {
    return NO;
}

-(void)drawRect:(CGRect)aRect {
    //Custom drawing code
}

此外,当我使用UITableViewCell的官方'backgroundView'属性时,上面的代码工作正常。

3 个答案:

答案 0 :(得分:1)

尝试

@property (nonatomic,retain) CellBackgroundView *customBackgroundView;

而不是strong,看看它是否有帮助......这似乎是一个记忆分配问题

答案 1 :(得分:1)

最后......答案确实是Xcode坏了。

或者更确切地说,调试器是。无论我将该变量设置为什么,Xcode都拒绝在调试器中显示。但是,代码实际上可以正常工作。

请修好Apple。

答案 2 :(得分:0)

我将类型更改为UIView。我带走了财产并合成了声明。我'清理'了这个项目。

所以我的代码现在只是:

@interface CustomCell : UITableViewCell {

    UIView *customBackgroundView;

}

// m-file

customBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];


结果:它不起作用。 customBackgroundView仍然是零。我的结论是,我要么疯了,要么X Code坏了。