我有一個UITableView
,其中UIImage
作爲子視圖添加到它的每個單元。圖像是PNG透明的。問題是當我滾動UITableView
,圖像重疊,然後我收到內存警告和東西。iOS - 何時從UITableViewCell中刪除子視圖
這裏是爲配置單元當前代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
int cellNumber = indexPath.row + 1;
NSString *cellImage1 = [NSString stringWithFormat:@"c%i.png", cellNumber];
UIImage *theImage = [UIImage imageNamed:cellImage1];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
return cell;
}
我知道去除UIImage
子視圖會是這樣的代碼:
[cell.imageView removeFromSuperview];
但我不知道在哪裏把它。我把它放在所有的線上;甚至在if語句中添加了一個else。似乎沒有工作!
謝謝!有效。但是當我滾動時,我仍然收到內存警告,導致一個CRASH! :(我在tableView中有500行左右,我不知道如何解決這個問題,有什麼想法嗎? – Milad 2012-07-06 09:33:49
我猜想圖像需要以某種方式'無效'!我使用儀器,當我滾動時,內存使用情況get更高的n更高 – Milad 2012-07-06 09:38:14
'cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];'你應該在創建上面的行時提及autorelease,所以正確的語句是這樣的:'cell = [[[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];' – EEJ 2012-08-29 09:48:59