2012-07-06 123 views
0

我有图像加载到UITableViewCell中。图像尺寸不是很大 - 每个图像大概只有6 KB。iOS - UITableView崩溃

但是有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; 
} 
+0

//我正在使用ARC(自动引用计数)。 我重新启动了该设备,崩溃有点问题得到解决。但内存使用率仍然变高(仪器显示它) – Milad 2012-07-06 10:30:13

回答

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

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

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

这是怎么回事?你重新分配两次相同的内存。

+0

我正在使用ARC(自动引用计数),它不允许我使用: return [cell autorelease]; – Milad 2012-07-06 10:29:06

+0

为什么在初始实例化后两次重新分配单元格? – 2012-07-06 10:32:57

+0

对不起,这是一个错字!修正了这个问题。 (不在代码中) – Milad 2012-07-06 14:56:49