使用 loadNibNamed 加载 UITableViewCell 时保留计数
我正在使用 Apple 加载自定义nib 文件中的 UITableViewCell:
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
// (1)
[[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
// (2)
cell = self.itemCell;
self.itemCell = nil;
// (3)
// code continues here
}
视图控制器的类声明:
@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
@private
UITableView *tableView;
ItemCell *itemCell;
}
@property (nonatomic, retain) IBOutlet ItemCell *itemCell;
MyViewController 是 ItemCell 的文件所有者。
我观察到以下情况:
- (1)
self.itemCell
保留计数为 0 - (2)
self.itemCell
保留计数为 2 - (3)
self.itemCell< /code> 保留计数为 0
- (3)
cell
保留计数为 1
有人可以解释一下:
- 为什么
self.itemCell
保留计数在 ( 之间从 2 变为 0 2) 和(3)? - 为什么(3)中
cell
的保留计数等于1?
I am using loadNibNamed:owner:options:
as documented by Apple to load a custom UITableViewCell from a nib file:
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
// (1)
[[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
// (2)
cell = self.itemCell;
self.itemCell = nil;
// (3)
// code continues here
}
And the class declaration of the view controller:
@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
@private
UITableView *tableView;
ItemCell *itemCell;
}
@property (nonatomic, retain) IBOutlet ItemCell *itemCell;
MyViewController is the File's Owner of the ItemCell.
I am observing the following:
- (1)
self.itemCell
retain count is 0 - (2)
self.itemCell
retain count is 2 - (3)
self.itemCell
retain count is 0 - (3)
cell
retain count is 1
Could someone explain:
- Why does
self.itemCell
retain count go from 2 to 0 between (2) and (3)? - Why is the retain count of
cell
in (3) equal to 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留计数没有用。别叫它。
至于你的两个问题的答案,“实施细节”。
只要您平衡保留和释放,您的工作就完成了。解释为什么保留计数是任何给定的绝对值需要访问框架本身的实现。
retainCount is useless. Don't call it.
As for the answer to your two questions, "implementation detail".
As long as you balance your retains and releases, your job is done. Explaining why the retain count is any given absolute value would require access to the implementation of the frameworks themselves.
糟糕,犯了一个错误,在 (3) 中
在 (3) 中,我调用
[self.itemCell keepCount]
来查看保留计数,但由于self.itemCell
设置为nil
已经,我得到的显然是 0!不知道我是如何错过的......在(3)中,
cell
保留计数为1,这是正常的(单元格由loadNibNamed:owner:options:
返回的数组保留代码>)Ooops, made a mistake, in (3)
In (3) I was calling
[self.itemCell retainCount]
to view the retain count but sinceself.itemCell
was set tonil
already , all I was getting was 0 obviously!! Not sure how I missed that...in (3),
cell
retain count is 1 which is normal (the cell is retained by the array returned byloadNibNamed:owner:options:
)