2010-11-16 111 views
0

我添加自定义视图与下面的代码tableHeaderView:内存泄漏在哪里?

imageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    imageButton.frame = CGRectMake(120, 12, 64, 64); 
    imageButton.titleLabel.font = [UIFont systemFontOfSize:10]; 
    imageButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap; 
    imageButton.titleLabel.textAlignment = UITextAlignmentCenter; 
    [imageButton setTitle:NSLocalizedString(@"Choose\nPhoto", @"Choose\nPhoto") forState:UIControlStateNormal]; 
    [imageButton addTarget:self action:@selector(photoButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

    // add existing image, if any, to button 
    if (child.thumbnailImage != nil) { 
     [imageButton setBackgroundImage:child.thumbnailImage forState:UIControlStateNormal]; 
    } 

    // add button to view 
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(22, 12, 70, 70)]; 
    [headerView addSubview:imageButton]; 

    // add view to table header 
    self.tableView.tableHeaderView = headerView; 

内存泄漏是显示在上述用于headerView的UIView的ALLOC线。 我声明UIView和UIButton在头文件中,并在ViewDidUnload和dealloc中释放它,所以我不知道我在做什么错了。 此外,这只是显示在设备上,而不是模拟器(只是想我会提到)。

任何帮助,将不胜感激。

感谢,

+0

您可以发布您ViewDidUnload? – xil3 2010-11-16 08:21:50

回答

1

您使用的是headerView setter方法(self.headerView),所以你需要释放你分配给headerView财产UIView的情况下,无论是使用releaseautorelease

例如

UIView* newHeaderView = [[UIView alloc] initWithFrame...]; 
self.headerView = newHeaderView; 
[newHeaderView release]; 

self.headerView = [[[UIView alloc] initWithFrame:...] autorelease]; 

的原因是因为所述headerView setter方法自动保留分配给它的对象。

或者,你可以直接设置headerView实例变量,而无需使用属性setter方法:

[headerView release]; // release any existing object 
headerView = [[UIView alloc] initWithFrame:...];