将按钮添加为子视图时,为什么会出现内存泄漏?

Jim*_*des 4 iphone instruments uibutton uitableview uikit

我有一个使用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;
}
Run Code Online (Sandbox Code Playgroud)

我认为一切都很好,直到我开始使用Instruments来确保我没有任何内存泄漏,但我发现将UIButton添加为单元格的子视图就像在某种程度上导致UIKit内部泄漏.

具体来说,我得到每个单元格行的内存泄漏(每次按钮被添加为子视图),泄漏的对象是"CALayer",负责框架为" - [UIView _createLayerWithFrame:]".

我在这里做错了吗?

Jus*_*tin 5

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

rectangle = CGRectMake(2.0f,2.0f,40.0f,40.0f);
checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
checkButton.frame = rectangle;
Run Code Online (Sandbox Code Playgroud)