initWithFrame 与 initWithStyle
我想从已弃用的 initWithFrame:reuseIdentifier:
更新我的 TableView。
我的表格视图使用自定义单元格。
它到处都说要使用 initWithStyle:
,并且它不会以任何方式从 initWithFrame:CGRectZero reuseIdentifier:
改变行为或单元格。
但是,当我使用 initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:
进行构建时,单元格变为空白(即我们的自定义单元格不起作用(因为它是用某种样式初始化的?))。
单元格初始化后(如果它没有出列),我们在单元格上设置文本。但是当我使用 initWithStyle:reuseIdentifier:
时,这些没有设置,但它与 initWithFrame:CGRectZero
一起使用。除了使用的 init 方法 (initWithStyle
) 之外,其他代码均未更改。
创建(或重用)单元格后放入的这些行:
cell.newsItemNameLabel.text = @"test";
NSLog(@"NewsItemName: %@",cell.newsItemNameLabel.text);
结果为“NewsItemName:(null)”
有人有想法吗?两者之间真正的区别是什么?
谢谢
I want to update my TableView from the deprecated initWithFrame:reuseIdentifier:
.
My tableview uses custom cells.
Everywhere it says to use initWithStyle:
, and that it does not change behavior or the cell in any way from initWithFrame:CGRectZero reuseIdentifier:
.
But when I am building with initWithStyle:UITableViewCellStyleDefault reuseIdentifier:
the cells become blank (i.e. our custom cell does not work (because it is initialized with some style?)).
After the cell has been initialized (if it did not dequeue), we set texts on the cell. But these are not set when I use initWithStyle:reuseIdentifier:
but it works with initWithFrame:CGRectZero
. None of the code is changed except for the init method used (initWithStyle
).
These rows put in after cell is created (or reused):
cell.newsItemNameLabel.text = @"test";
NSLog(@"NewsItemName: %@",cell.newsItemNameLabel.text);
Results in "NewsItemName: (null)"
Anybody has an idea? What is the real difference between the two?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cellForRowAtIndexPath
的实现应类似于以下内容:其中
CustomCell
是自定义单元格类的名称。请注意,此实现使用 ARC(自动引用计数)。如果您没有碰巧使用此功能,请在单元格分配中添加autorelease
调用。CustomCell
的initWithStyle
实现:Your implementation of
cellForRowAtIndexPath
should look similar to the following:where
CustomCell
is the name of your custom cell's class. Note that this implementation uses ARC (Automatic Reference Counting). If you don't happen to use this feature, add aautorelease
call to the allocation of your cell.CustomCell
'sinitWithStyle
implementation: