从nib问题加载自定义单元格

时间:2011-10-27 16:16:32

标签: iphone objective-c ios uitableview

我的自定义CustomTableViewCell : UITableViewCell位于@property(nonatomic, retain) Model* model;。还有一个带有一个视图的xib文件(具有正确配置的类和重用标识符的单元)。在我的自定义单元格类中,有一个- init实现:

- (id)init
{
    NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
    CustomTableViewCell* cell = [[array firstObject] retain];
    self = cell;

    self.model = [[Model alloc] init] autorelease];
    self.model.someString = @"foo";

    // here I can access self.model with no problem
    return self;
    // after method return, self.model refers to 0x0
}

我的问题是... - init返回后,self.model为零。单元格正在工作,但没有model。我不知道为什么。有任何想法吗?我想,我的- init不是犹太人......

修改

我发现,返回后模型为nil no,但是在调用表视图数据源方法时。表视图也指当时的0x0让我惊讶的是什么。完全奇怪!谢谢你的努力,我将用代码做我的单元格......

3 个答案:

答案 0 :(得分:2)

您是否在实施中合成了模型?

@synthesize model;

答案 1 :(得分:2)

您不应在init方法中使用dot属性语法。基本上是因为整个对象可能尚未完全设置。试试这个:

model=[Model alloc] init];
model.someString=@"foo";

你也应该在用你的ivars做其他事情之前检查一下self是否为零

祝你好运。

答案 2 :(得分:2)

  1. 您需要检查[Model alloc] init]的结果,确保值不是nil。
  2. 检查单元格的属性model,确保它未分配。
  3. 如果model的{​​{1}}仍为零,请在此处发布更多代码。
相关问题