简体   繁体   English

保留tableView:cellForRowAtIndexPath的计数:

[英]Retain count for tableView:cellForRowAtIndexPath:

In Apple's example code, the method tableView:cellForRowAtIndexPath: of a UITableViewDataSource returns a cell with a retain count of 1; 在Apple的示例代码中, UITableViewDataSource tableView:cellForRowAtIndexPath:方法返回保留计数为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. 但是,静态分析器抱怨这违反了Cocoa命名约定,因为方法名不是以'new'开头,等等。文档中没有提及该单元格的预期保留数。 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. 进一步编辑:如果提问者因在问题中使用其术语而被跳过,则可能应针对Clang提交一个错误。 :-) :-)

The value of retainCount is not really important (it can go up and down for seemingly unknown reasons). retainCount的值并不是很重要(它可能会因似乎未知的原因而上升和下降)。 But cells created in tableView:cellForRowAtIndexPath: should be autoreleased. 但是在tableView:cellForRowAtIndexPath:创建的单元格应该被自动释放。 What example code are you looking at? 您在看什么示例代码?

Which example code? 哪个示例代码? MyTableViewController.m returns either [tableView dequeueReusableCellWithIdentifier:kCellID] or [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease] . MyTableViewController.m返回[tableView dequeueReusableCellWithIdentifier:kCellID][[[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; 几乎所有方法都遵循Objective-C命名约定; the ones that don't tend to be explicitly documented. 那些没有被明确记录的文件。

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. 保留计数始终至少为1。您永远都不会取回保留计数小于该值的对象,因为它已经是前对象了。 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. 做正确的事情,不要为其余的事情烦恼。

In fact, DO NOT USE THE retainCount at all. 实际上,根本不要使用keepCount。 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! 最好使用Leak或Zombie工具!

(ps also thanks to walkytalky - as I just see he also answered this one!) (ps也要感谢walkytalky-我刚刚看到他也回答了这个问题!)

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. alloc一个UITableViewCell在你cellForRowAtIndexPath:这意味着你必须将其释放,或者你有内存泄漏。 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. 因此,您可以autorelease它,以使自动释放池在以后释放它。 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). 当您返回它时,它尚未被释放,但是稍后会被系统释放(您只是想要放弃它的所有权,这是您想要的,因为在返回它之后,您无需维护对该单元的引用从功能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM