2012-07-06 121 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