iOS-何时从UITableViewCell删除子视图

iOS-何时从UITableViewCell删除子视图,uitableview,uiimage,subview,Uitableview,Uiimage,Subview,我有一个UITableView,其中一个UIImage作为子视图添加到每个单元格中。图像是PNG透明的。问题是,当我滚动查看UITableView时,图像会重叠,然后我会收到内存警告和其他内容 以下是配置单元格的当前代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifi

我有一个
UITableView
,其中一个
UIImage
作为子视图添加到每个单元格中。图像是PNG透明的。问题是,当我滚动查看
UITableView
时,图像会重叠,然后我会收到内存警告和其他内容

以下是配置单元格的当前代码:

- (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。好像不管用

UITableViewCell
已附加图像视图。删除:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
加:

你可以走了

结果代码如下所示:

- (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];
    }

    UIImage *cellImage = [UIImage imageNamed:[NSString stringWithFormat:@"c%i.png", indexPath.row + 1]];
    [cell.imageView setImage:cellImage];

    return cell;
}
您只需点击以下按钮:

 [cell.imageview setImage:theImage];
并删除:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];

谢谢成功了。但是,当我滚动时仍会收到内存警告,导致崩溃!!:(我在tableView中有大约500行。我不知道如何解决这个问题。有什么想法吗?我想这些图像需要以某种方式得到“零”!我使用了工具,当我滚动时,内存使用量会越来越高。
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
您应该在创建上述行时提到自动释放。因此正确的语句如下:
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];