0

我将图像加载到 UITableViewCell 中。这些图像的大小并不大——每张可能只有 6 KB。

但是有 500 行。当我滚动时,它一开始很慢,并给我内存警告消息,但再滚动几次后,应用程序崩溃了。我也使用了一些工具,它表明当我滚动时内存使用率越来越高!dequeueReusableCellWithIdentifier:CellIdentifier是不是工作不正常,或者我应该以某种方式“消除”图像?!

顺便说一句,我已经关闭了我设备上的所有其他应用程序,并且我的 iPod Touch 上有超过 1.5 GB 的可用空间。

这是配置单元格的代码:

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

    [cell.imageView setImage:theImage];

    return cell;
}
4

1 回答 1

0
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier];
}

UITableViewCell *cell = [[UITableViewCell alloc] 
                         initWithStyle:UITableViewCellStyleDefault 
                         reuseIdentifier:CellIdentifier];

这里发生了什么?您正在重新分配相同的内存两次。

于 2012-07-06T10:18:54.010 回答