从笔尖问题加载自定义单元格
我有我的自定义 CustomTableViewCell : UITableViewCell
和 @property(nonatomic, keep) 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
为零。单元格正在工作,但没有模型
。我不知道为什么。有什么想法吗?我认为,我的 - init
不合规矩...
编辑
我发现,返回后模型为零,但是当调用表视图数据源方法时。当时表视图指的是 0x0,这让我非常惊讶。太奇怪了!感谢您的努力,我将用代码来完成我的单元格......
I have my custom CustomTableViewCell : UITableViewCell
with @property(nonatomic, retain) Model* model;
. There is also xib file with one view (cell with properly configured class and reuse identifier). In my custom cell class, there is an - init
implementation:
- (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
}
My problem is... after the returning of the - init
, self.model
is nil. Cell is working, but without model
. And I don't know why. Any ideas? I think, my - init
is not kosher...
EDIT
I found out, the model is nil no after return, but when table view data source methods are called. Also table view refers to 0x0 in that time what totally amazes me. Totally weird! Thank you for your endeavor, I'm going to do my cell in code...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在实施中综合了模型吗?
Have you synthesised model in the implementation?
您不应该在 init 方法中对 ivars 使用点属性语法。基本上是因为整个对象可能还没有完全设置好。试试这个:
在你做任何其他事情之前,你也应该检查一下 self 是否不为零,祝你
好运。
you should not use dot property syntax for your ivars in your init method. Basically because the entire object may not be completely setup up yet. Try this:
also you should check to see if self is not nil before going doing anything else with your ivars
good luck.
[Model alloc] init]
的结果,确保值不为零。model
,确保它未分配。cell
的model
仍然为零,请在此处发布更多代码。[Model alloc] init]
, make sure value is not nil.model
of the cell, make sure it's not assign.model
ofcell
is nil, please posts more codes here.