【发布时间】:2012-01-30 17:55:46
【问题描述】:
我正在填充数据的表格视图中有泄漏。
这是发生了什么:
我有一个NSMutableArray 填充了NSDictionaries(联系信息),每次用户选择一个新的索引字母时,我都会从sql 数据库加载它,数组中填充了这些联系人的信息。
由于我使用实例变量数组来保存数据,因此在每次交换新数据之前,我都会想到这样的事情:
if (currentConnections != nil) {
[currentConnections release];
currentConnections = nil;
}
在我重置之前会有效地删除我的旧数据。
然而,这就是奇怪的开始。在我的tableView:cellForRowAtIndexPath: 中,我会这样打电话:
NSString *firstname = [[currentConnections objectAtIndex:indexPath.row]
objectForKey:@"firstname"];
UILabel *name = [[UILabel alloc]
initWithFrame:CGRectMake(75, 12, 190, 15)];
name.font = [UIFont fontWithName:@"Gotham-Medium.ttf"
size:12];
name.textColor = [UIColor whiteColor];
name.backgroundColor = [UIColor clearColor];
name.text = firstname;
其中当前连接是字典数组。然后我将UILabel 添加为单元格的子视图:
[cell addSubview:name];
[name release];
这样的行为是这样的:
当我按原样运行时(在每次交换之前释放当前连接,然后将新数据加载到其中),应用程序将因访问错误而崩溃。
当我不释放我添加到单元格的子视图时,应用程序不会崩溃,但我惊恐地看到实时字节继续在内存分配工具中累积,直到我切换联系信息的程度足以我收到内存警告然后崩溃.
在我看来,单元格和子视图存在一些奇怪的内存所有权问题。我确实将单元格指定为可重用并在加载每个单元格数据之前将其分配给自动释放。
对可能导致此泄漏的原因有什么想法吗?
编辑:
这是当前的 tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if (cell == nil) {
NSLog(@"within");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
if (!ALOCATED) {
cellName = [[ UILabel alloc ]
initWithFrame:CGRectMake(75, 12, 190, 15)
];
cellName.font = [ UIFont
fontWithName:@"Gotham-Medium.ttf"
size:12
];
cellName.textColor = [ UIColor whiteColor ];
cellName.backgroundColor = [ UIColor clearColor ];
NSString *firstname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"firstname"
];
firstname = [ firstname stringByAppendingString:@" " ];
NSString *lastname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"lastname"
];
UIImage *profPic = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"profilepicture"
];
connectionPhoto = [[ UIImageView alloc ] initWithFrame:CGRectMake(10, 8, 56, 56) ];
connectionPhoto.image = profPic;
NSString *fullname = [ firstname stringByAppendingString:lastname ];
cellName.text = fullname;
[ cell addSubview:cellName ];
cellTitle = [[ UILabel alloc ]
initWithFrame:CGRectMake(75, 31, 260, 13)
];
cellTitle.font = [ UIFont
fontWithName:@"ArialMT"
size:10
];
cellTitle.textColor = [ UIColor whiteColor ];
cellTitle.backgroundColor = [ UIColor clearColor ];
cellTitle.text = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"title"
];
[ cell addSubview:cellTitle ];
[ cell addSubview:connectionPhoto ];
[ profPic release ];
ALOCATED = YES;
} else {
cellTitle.text = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"title"
];
connectionPhoto.image = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"profilepicture"
];
NSString *firstname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"firstname"
];
firstname = [ firstname stringByAppendingString:@" " ];
NSString *lastname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"lastname"
];
NSString *fullname = [ firstname stringByAppendingString:lastname ];
cellName.text = fullname;
}
return cell;
}
我试图创建一个单元格并重用它,每次只更改其子视图的属性。
编辑
发生的事情是我正在向单元对象本身添加子视图,这导致了泄漏,因为这些没有被释放。一旦我开始将视图添加到 UITableViewCell 成员视图,即 cell.contentView、cell.accessoryView 等......内存就被释放了。所以正如下面提到的丹尼尔,要么创建一个自定义表格单元格并将您的视图添加到您的单元格属性中,要么将您的视图添加到成员 uitablecellviews 而不是单元格本身。以下对我有用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
UILabel *cName = [[ UILabel alloc ]
initWithFrame:CGRectMake(105, 10, 190, 15)
];
cName.font = [ UIFont
fontWithName:@"Gotham-Medium.ttf"
size:12
];
cName.textColor = [ UIColor whiteColor ];
cName.backgroundColor = [ UIColor clearColor ];
NSString *firstname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"firstname"
];
firstname = [ firstname stringByAppendingString:@" " ];
NSString *lastname = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"lastname"
];
NSString *fullname = [ firstname stringByAppendingString:lastname ];
cName.text = fullname;
[ cell.contentView addSubview:cName ];
[ cName release ];
cell.imageView.image = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"profilepicture"
];
UILabel *cTitle = [[ UILabel alloc ]
initWithFrame:CGRectMake(105, 25, 190, 13)
];
cTitle.font = [ UIFont
fontWithName:@"ArialMT"
size:10
];
cTitle.textColor = [ UIColor whiteColor ];
cTitle.backgroundColor = [ UIColor clearColor ];
cTitle.text = [[ currentConnections
objectAtIndex:indexPath.row
] objectForKey:@"title"
];
[ cell.contentView addSubview:cTitle ];
[ cTitle release ];
return cell;
}
【问题讨论】:
-
呃?如何将 NSString 添加为子视图
-
我刚刚编辑了它。哎呀,这是我添加为子视图的标签
标签: iphone objective-c ios uitableview memory