为什么自定义的基于 nib 的表格单元格的 init 方法没有被调用
我有一个在界面生成器中创建的基于笔尖的表格视图单元格。我将表视图单元格的类设置为从 UITableViewCell
扩展的 FooTableViewCell
。
在 FooTableViewCell 中,我像这样重写了 init 方法:
-(id)init{
if ((self = [super init])){
// My init code here
}
return self;
}
我现在期望在实例化它时调用 my 方法。然而,表视图被显示,但该方法从未被调用。
我可以解决这个问题,但我想完全理解它,对我来说,不清楚一个对象如何在不调用 init
方法的情况下生存。
I have a nib-based table view cell which I created in Interface builder. I set the class of the table view cell to FooTableViewCell
which extends from UITableViewCell
.
In FooTableViewCell
I override the init method like this:
-(id)init{
if ((self = [super init])){
// My init code here
}
return self;
}
I now expected that my gets called, when it is being instantiated. However the table view gets displayed but the method is never called.
I could work around this but I would like to fully understand it and for me it's not clear how an object can come to live without the init
method being called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当未归档时,初始化会经历稍微不同的路径。
将调用
-(id)initWithCoder:(NSCoder*)aDecoder
,而不是调用-(id)init
。此时插座尚未连接,如果您需要访问插座,您可以覆盖-(void)awakeFromNib
,它将在对象连接后调用。When being unarchived initialization goes through a slightly different path.
Instead of
-(id)init
being called-(id)initWithCoder:(NSCoder*)aDecoder
will be called. At this point outlets aren't hooked up, if you need access to the outlets you can override-(void)awakeFromNib
which will be called after the object has been hooked up.当从 nib 文件加载对象时,会调用它的 -awakeFromNib 方法 - 您可以将初始化代码放在那里:
When object is being loaded from nib file then its -awakeFromNib method is called - you can put your initialisation code there: