2012-04-09 23 views
0
The uitableview view gets slow while receiving images from server through Json 

' - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {的UIImage通過JSON負載中的UITableView慢

NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 
    UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleSubtitle 
              reuseIdentifier:CellIdentifier] autorelease];; 
    } 

    NSURL *url = [NSURL URLWithString:[dict objectForKey:@"allfeeds3"]]; 
    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
// cell.imageView.image = [UIImage imageWithData:data]; 
    UIImage *tmpImage = [[UIImage alloc] initWithData:data]; 
    cell.imageView.image = tmpImage; 

} 
+0

這已經在這裏問SOOOOOOOO很多次......請你只可以發佈您的問題之前搜索。答案已經在這裏... – 2012-04-09 15:28:47

+1

選中此項:[異步加載圖像](http://stackoverflow.com/questions/5840451/asynchronous-loading-of-images),[將圖像加載到UIImage中異步](http ://stackoverflow.com/questions/9786018/loading-an-image-into-uiimage-asynchronously) – Lucien 2012-04-09 15:31:01

回答

1

我相信同樣的問題之前已經被問過,而且很可能是由於你的代碼非常相似。

你正在創建一個新的單元中的每個數據源是要求其委託一次。此行代碼:

NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 

導致每次都創建一個新的單元格。如果每個細胞是相同的(在佈局,而不是內容方面),您可以使用一個靜態的NSString的重用標識符。這樣,tableview會嘗試從隊列中拉出一個可重用的單元格,而不是每次創建一個單元格。

此外,它看起來像你想剛纔下載的主線程上的數據。並且你分配/初始化一個新的UIImageView,即使單元格被重用。你需要繼承的UITableViewCell,要麼建立在layoutSubviews中的UIImageView,或從筆尖加載它。你需要重新思考你的整個解決方案。小心。

+0

https://github.com/rs/SDWebImage – 2012-04-09 19:33:33

+0

SDWebImage是偉大的,但不改變上述幾點。 – jmstone617 2012-04-09 19:46:04