我有一个使用 tableview 的应用程序,以及作为子视图添加到每个自定义单元格的 UIButton,如下所示:
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 添加为单元格的子视图会导致 UIKit 中的泄漏。
具体来说,我得到每个单元格行的内存泄漏(每次将按钮添加为子视图),泄漏的对象是“CALayer”,负责的框架是“-[UIView _createLayerWithFrame:]”。
我在这里做错了吗?