I'm adding a custom view to a tableHeaderView with the following code:
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 (chi开发者_开发知识库ld.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;
The memory leak is showing on the alloc line above for the headerView UIView. I'm declaring the UIView and the UIButton in the header file and releasing it in ViewDidUnload and dealloc, so I'm not sure what I'm doing wrong. Also, this is only showing up on the device and not the simulator (just thought I'd mention).
Any help would be appreciated.
Thanks,
Rod
You're using the headerView setter method (self.headerView
) so you need to release the UIView instance you assign to the headerView property, either using release
or autorelease
.
e.g.
UIView* newHeaderView = [[UIView alloc] initWithFrame...];
self.headerView = newHeaderView;
[newHeaderView release];
or
self.headerView = [[[UIView alloc] initWithFrame:...] autorelease];
The reason is because the headerView
setter method automatically retains the object assigned to it.
Alternatively, you can set the headerView instance variable directly, without using the property setter method:
[headerView release]; // release any existing object
headerView = [[UIView alloc] initWithFrame:...];
精彩评论