保留 tableView 的计数:cellForRowAtIndexPath:

发布于 2024-09-14 02:38:26 字数 345 浏览 4 评论 0原文

在 Apple 的示例代码中,UITableViewDataSource 的方法 tableView:cellForRowAtIndexPath: 返回保留计数为 1 的单元格;它分配它,但不会自动释放它。然而,静态分析器抱怨这违反了 Cocoa 命名约定,因为方法名称不以“new”开头等。文档没有提及单元格的预期保留计数。细胞应该有多少保留计数?我应该针对文档提交错误吗?谢谢。

编辑:我查看的示例代码会自动释放它,而我的眼睛不知何故跳过了它。抱歉浪费您的时间。感谢您的回复。

进一步编辑:如果提问者因在问题中使用 Clang 术语而被跳过,则可能应该针对 Clang 提交错误。 :-)

In Apple's example code, the method tableView:cellForRowAtIndexPath: of a UITableViewDataSource returns a cell with a retain count of 1; it allocs it, but doesn't autorelease it. However, the static analyzer complains that this violates the Cocoa naming conventions, since the method name doesn't start with 'new', etc. The documentation doesn't mention the expected retain count of the cell. What retain count should the cell have? Should I file a bug against the documentation? Thanks.

EDIT: The example code I looked at does autorelease it, and my eye somehow skipped over it. Sorry to waste your time. Thanks for the responses.

Further edit: A bug should probably be filed against Clang if questioners are going to get jumped for using its terminology in a question. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请止步禁区 2024-09-21 02:38:26

retainCount 的值并不重要(它可能会因为看似未知的原因而上下波动)。但是在 tableView:cellForRowAtIndexPath: 中创建的单元格应该自动释放。您正在查看什么示例代码?

The value of retainCount is not really important (it can go up and down for seemingly unknown reasons). But cells created in tableView:cellForRowAtIndexPath: should be autoreleased. What example code are you looking at?

油饼 2024-09-21 02:38:26

哪个示例代码? MyTableViewController.m 返回 [tableView dequeueReusableCellWithIdentifier:kCellID][[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease]

如果示例代码执行了不同的操作,则可能是错误的。几乎所有方法都遵循 Objective-C 命名约定;那些往往没有明确记录的内容。

Which example code? MyTableViewController.m returns either [tableView dequeueReusableCellWithIdentifier:kCellID] or [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease].

If the example code does something different, it's probably wrong. Nearly all methods follow Objective-C naming conventions; the ones that don't tend to be explicitly documented.

Oo萌小芽oO 2024-09-21 02:38:26

保留计数始终至少为 1。您永远不会取回保留计数小于该值的对象,它已经是一个前对象了。请不要从保留计数中得出结论,或者对它们抱有期望,甚至不要查看它们。从来没有从来没有从来没有。

可能到处都有一些狡猾的示例代码做了错误的事情。忽略它。做正确的事,不要为其余的事情烦恼。

Retain count is always at least 1. You won't ever get back an object with a retain count less than that, it would be an ex-object already. Please please please don't draw conclusions from retain counts, or have expectations about them, or even ever look at them. Never never never never never.

There may possibly be dodgy example code here and there that does the wrong thing. Ignore it. Do the right thing and don't fret yourself about the rest.

心作怪 2024-09-21 02:38:26

事实上,根本不要使用retainCount。我很困惑,它把我引向了完全错误的方向,我浪费了好几天的时间寻找错误的漏洞。如果计数增加或减少,这绝对没有任何意义!不要浪费一秒钟的时间来处理它。

使用 Leak 或 Zombie 工具要好得多!

(另外还要感谢walkytalky - 正如我刚刚看到他也回答了这个问题!)

In fact, DO NOT USE THE retainCount at all. I got so confused and it lead me into the totally wrong direction and I wasted literally days hunting down wrong leaks. It means ABSOLUTELY NOTHING if the count goes up or down! Don't waste a second dealing with it.

It's much better to use the Leak or Zombie tools!

(ps also thanks to walkytalky - as I just see he also answered this one!)

°如果伤别离去 2024-09-21 02:38:26

不用担心保留计数。您在 cellForRowAtIndexPath:分配一个UITableViewCell,这意味着您必须释放它,否则就会出现内存泄漏。您无法释放它,因为您必须返回单元格,让表视图将其绘制为子视图,然后释放它。因此,您autorelease它可以让自动释放池稍后释放它。当您返回它时,它还没有被释放,但稍后会被系统释放(您只是放弃了它的所有权,这正是您想要的,因为您在返回后不维护对单元格的引用来自函数)。

Don't worry about the retain count. You alloc a UITableViewCell in your cellForRowAtIndexPath:, which means you have to release it or you have a memory leak. You can't release it because you have to return the cell, have the table view draw it as a subview, then release it. Therefore you autorelease it to have the autorelease pool release it later. When you return it, it hasn't been released yet, but gets released later by the system (you've simply relinquished ownership of it, which is what you want, because you don't maintain a reference to the cell after it's returned from the function).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文