gpt4 book ai didi

iphone - 为什么将按钮添加为 subview 时会出现内存泄漏?

转载 作者:行者123 更新时间:2023-12-03 19:12:54 25 4
gpt4 key购买 nike

我有一个使用 tableview 的应用程序,以及一个 UIButton,我将其作为 subview 添加到每个自定义单元格,如下所示:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(2.0, 2.0, 40.0, 40.0)];
[cell.contentView addSubview:checkButton];

// lot's of other code

return cell;
}

我认为一切都很好,直到我开始使用 Instruments 来确保没有任何内存泄漏,但我发现将 UIButton 添加为单元格的 subview ,这样会以某种方式导致 UIKit 内的泄漏。

具体来说,我得到每个单元格行的内存泄漏(每次将按钮添加为 subview 时),泄漏的对象是“CALayer”,负责的框架是“-[UIView _createLayerWithFrame:]”。

我在这里做错了什么吗?

最佳答案

代码 [UIButton buttonWithType] 方法已包含 initWithFrame 方法。您只需使用 CGRectMake,然后设置按钮的框架即可。

rectangle = CGRectMake(2.0f,2.0f,40.0f,40.0f);
checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
checkButton.frame = rectangle;

关于iphone - 为什么将按钮添加为 subview 时会出现内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2313377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com