我在我的表格中有泄漏,我正在填充数据。UITableView单元内存泄漏
这里是正在发生的事情:
我已经充满了NSDictionaries
(联系人信息)的NSMutableArray
,我从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];
从这个行为是这样的:
当运行如(每个交换之前释放当前连接然后将新数据加载到它)该应用程序会因访问错误而崩溃。
当我不发布子视图时,我添加到单元格的应用程序不会崩溃,但我看着恐怖的活字节继续积累在内存分配工具中,直到我切换联系信息的时候,我得到一个内存警告然后崩溃。
在我看来,在单元格和子视图中会出现一些奇怪的内存所有权问题。我确实将单元格指定为可重用的,并在将每个数据加载之前将其指定给autorelease。
任何可能导致此泄漏的想法?
编辑:
这是当前的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成员视图aka 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作为子视图 – Daniel 2012-01-30 17:58:09
我刚刚编辑它。哎呀是这是一个标签,我添加为子视图 – 2012-01-30 19:01:40