将 @Property UISwitch 添加到 TableView 会导致 RetainCount 问题

发布于 2024-09-28 10:37:48 字数 743 浏览 3 评论 0原文

我正在使用 UISwitchs 和 UITextFields 执行此操作...

我已在头文件中将 UISwitch 声明为属性,因为我想在类中的几种不同方法中访问它的值。

我使用以下代码将 UISwitch 添加到我的 TableViewCell 之一:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
mySwitch.on = YES;

return cell;

} 

事实上,retainCounts 遍布各处。当单元格被放置在屏幕上时,mySwitch Retain 为 2,每次我点击开关,保留计数就会增加,直到达到 4,然后它似乎就停留在那里。

显然我错过了一些东西。如果有人能指出我正确的方向,我将不胜感激。

I am doing this with UISwitchs and UITextFields...

I have declared the UISwitch as Property in the header file, because I want to access its value in several different methods within my class.

I am adding the UISwitch to one of my TableViewCells, using the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
mySwitch.on = YES;

return cell;

} 

As it is, the retainCounts are all over the place. The mySwitch Retain is 2 by the time the cell has been put on the screen, and every time I tap the switch, the retain count goes up, until it gets to 4, then it seems to stay there.

Obviously I am missing something. If someone can point me in the right direction, it would be greatly appreciated.

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

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

发布评论

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

评论(2

七婞 2024-10-05 10:37:48

每当私有 api 与对象交互时,您都不想追逐对象的保留计数。您要做的就是跟踪您自己的保留计数,然后确保根据需要递增和递减它。假设您有如下内容:

@property (nonatomic, retain) UISwitch *mySwitch;

@synthesize mySwitch;

您应该将上面的代码更改为:

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
mySwitch.on = YES;

在上面的代码中,您不再拥有 mySwitch,因为您告诉它自动释放。但是,通过使用 self.mySwitch ,您在创建该属性时将保留该属性。然后您可以在程序的其余部分中随心所欲地使用它。只要确保在 dealloc 中安全地释放它即可。

Chasing the retainCount of an object is not something that you want to do whenever private api's are interacting with it. All that you want to do is to track your own retain counts and then make sure you are incrementing it and decrementing it as necessary. Assuming you have something like the following:

@property (nonatomic, retain) UISwitch *mySwitch;

@synthesize mySwitch;

You should change your code that you have above to be this:

self.mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
mySwitch.on = YES;

In the code you have above, you do not own mySwitch anymore as you told it to autorelease. However, by using self.mySwitch you will be retaining the property when you create it there. Then you can use it as you wish throughout the rest of the program. Just make sure to release it safely in dealloc.

っ左 2024-10-05 10:37:48

1:永远、永远、永远不要关注retainCount返回的内容。它并不意味着是人类可解释的值。

2:尝试运行构建和分析。它可以找到很多内存问题,比如这个,并解释出了什么问题。

3:每次调用alloc时,都需要一个匹配的release(或autorelease)。在这种情况下,您可以在 mySwitch.on = YES 之后调用 [mySwitch release]

4:定期检查 Objective-C 的内存管理规则是值得的。 http://developer.apple.com/库/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html

1: Never, ever, ever pay attention to what gets returned by retainCount. It is not meant to be a human interpretable value.

2: Try running build and analyze. It can find a lot of memory problems, such as this one, and explain what's wrong.

3: Every time you call alloc, you need a matching release (or autorelease). In this case, you could call [mySwitch release] after mySwitch.on = YES.

4: It pays to periodically review the memory management rules for Objective-C. http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html

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