Iphone [[NSBundle mainBundle]loadNibNamed:owner:options:]用于加载自定义表格单元格时是否会导致内存泄漏?
目前,我正在使用[[NSBundle mainBundle]loadNibNamed:owner:option:]为iPhone创建自定义表视图单元格,以加载nib。当我评测我的应用程序时,每次我使用这些自定义表格单元打开视图时,都会产生内存泄漏。这些工具将我指向[[NSBundle mainBundle]loadNibNamed:owner:option:]行。这是我的密码:Iphone [[NSBundle mainBundle]loadNibNamed:owner:options:]用于加载自定义表格单元格时是否会导致内存泄漏?,iphone,memory-leaks,custom-controls,Iphone,Memory Leaks,Custom Controls,目前,我正在使用[[NSBundle mainBundle]loadNibNamed:owner:option:]为iPhone创建自定义表视图单元格,以加载nib。当我评测我的应用程序时,每次我使用这些自定义表格单元打开视图时,都会产生内存泄漏。这些工具将我指向[[NSBundle mainBundle]loadNibNamed:owner:option:]行。这是我的密码: static NSString *challengeCellIdentifier = @"challengeListT
static NSString *challengeCellIdentifier = @"challengeListTableCell";
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ChallengeListTableCell *cell = (ChallengeListTableCell *) [tableView dequeueReusableCellWithIdentifier:challengeCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"ChallengeListTableCell"
owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[ChallengeListTableCell class]]){
cell = (ChallengeListTableCell *) currentObject;
break;
}
}
}
// Configure the cell...
if(indexPath.row == 0){ // top
[cell.backgroundImg setImage:[UIImage imageNamed:@"topMenuBar.png"]];
[cell.selectedBackgroundImg setImage:[UIImage imageNamed:@"topMenuBarOn.png"]];
}else if(indexPath.row == [challenges count]-1){ //bottom
[cell.backgroundImg setImage:[UIImage imageNamed:@"bottomMenuBar.png"]];
[cell.selectedBackgroundImg setImage:[UIImage imageNamed:@"bottomMenuBarOn.png"]];
}else{ //middle
[cell.backgroundImg setImage:[UIImage imageNamed:@"middleMenuBar.png"]];
[cell.selectedBackgroundImg setImage:[UIImage imageNamed:@"middleMenuBarOn.png"]];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //ipad
[cell.title setFont:[UIFont fontWithName:@"MuseoSans-500" size:40.0f]];
}else{ //iphone
[cell.title setFont:[UIFont fontWithName:@"MuseoSans-500" size:20.0f]];
}
Challenge * challenge = [challenges objectAtIndex:indexPath.row];
[cell.title setText:challenge.title];
if([challenge.completed boolValue]){
[cell.checkImage setImage: [UIImage imageNamed:@"checkComplete.png"]];
} else {
[cell.checkImage setImage: [UIImage imageNamed:@"checkNotComplete.png"]];
}
return cell;
文档指出,从loadNibNamed:owner:options:返回的数组是一个自动删除的对象,所以我不明白这为什么会导致泄漏。还有其他人有这个问题吗?您没有释放您分配的自动释放池。看起来设计很差 如果表视图无法回收单元格,则您正在加载NIB。。。 这是错误的,因为NIB可能会被加载,即使单元无法回收 然后,假设您的
for
循环将获得一个有效实例。无错误检查。坏
最后,分配一个自动释放池(在这种情况下不知道为什么)。你只是忘了发布它。编辑:autoreleasepool实际上在代码中被注释掉了,我的道歉。autoreleasepool实际上在代码中被注释掉了,我的道歉。我在我的帖子中更新了代码。以这种方式加载自定义nib来自苹果自己的自定义动态表格单元格示例,应该会返回自动释放的对象。我将在循环中添加错误检查,谢谢。自动删除池是我代码中的一个注释,我更新了我的帖子。