2013-05-13 118 views
2

嗨,我的問題是,當我滾動UITableView細胞圖像實際上這些圖像是在web服務器和我並使用asyncdownloading下載它。我在UITableViewCell上添加了UIImageView。圖像成功地顯示在UITableViewCell但是當我滾動的tableview圖像的變化是這樣的代碼單元格圖像更改滾動UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"]; 
     searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row]; 
    if(cell == nil) 
    { 
     cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"]; 



    } 

    UILabel *areaLbl = (UILabel *)[cell viewWithTag:1]; 
    UILabel *postTitle = (UILabel *)[cell viewWithTag:2]; 
    UILabel *roomCost = (UILabel *)[cell viewWithTag:3]; 
    UILabel *description = (UILabel *)[cell viewWithTag:4]; 
    areaLbl.text =searchobjectval.location; 
    postTitle.text = searchobjectval.title; 
    roomCost.text = searchobjectval.roomCost; 
    description.text =searchobjectval.description; 

    [tableView setSeparatorColor:[UIColor blueColor]]; 

    UIView *myView = [[UIView alloc] init]; 
    if (indexPath.row % 2) 
    { 
     UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1]; 
     myView.backgroundColor = clr; 
    } 
    else 
    { 
     myView.backgroundColor = [UIColor whiteColor]; 
    } 
    cell.backgroundView = myView; 

    NSLog(@" search object image %@",searchobjectval.imgLink); 


    // Pass along the URL to the image (or change it if you are loading there locally) 
    [AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData) 
    { 
     if (self.view.window) 
     { 
      UIImage *image = [UIImage imageWithData:imageData]; 
      UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10]; 
      imgVw.image = image; 
     } 
    }]; 




    return cell; 
} 

回答

0

您應該將圖像存儲爲上searchobjectval一個屬性,並將其設置在cellForRowAtIndexPath圖像視圖。當異步加載完成時,檢查圖像的目標單元格是否可見,如果是,則重新加載它。如果單元格不可見,則可以等待它再次變爲可見,然後cellForRowAtIndexPath將爲您設置它。

請記住,如果這些圖像很大並且self.arrSearchResult存在很長時間,那麼您應該在其中放置一些邏輯以清除內存中的圖像,如果它開始變慢。

+0

對從內存清除圖像擴大,'NSCache'會是一個更好的存儲類;那麼你不需要編寫任何邏輯。或者更好,不要重新發明輪子,並使用SDWebImage。 – 2013-05-13 14:09:28

2

對於內存管理目的的UITableView重用它的單元格UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];和向上或向下滾動時加載單元格內容。如果你不提取數據到單元格的元素,那麼當你回滾到最後一次使用單元格時,你將獲得最後一個值。

你的情況:

// Pass along the URL to the image (or change it if you are loading there locally) 
    [AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData) 
    { 
     if (self.view.window) 
     { 
      UIImage *image = [UIImage imageWithData:imageData]; 
      UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10]; 
      imgVw.image = image; 
     } 
    }]; 

所以當!self.view.window你就會有個最後使用單元格的圖像(如果子句返回TRUE)和滾動時將發生變化。你需要做的是:

// Pass along the URL to the image (or change it if you are loading there locally) 
    [AsyncImagesDownloading processImageDataWithURLString:[NSString stringWithFormat: @"http://192.95.48.72/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] andBlock:^(NSData *imageData) 
    { 
     if (self.view.window) 
     { 
      UIImage *image = [UIImage imageWithData:imageData]; 
      UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10]; 
      imgVw.image = image; 

     } else { 

      UIImage *image = [UIImage imageNamed:@"placeHolder.png"]; 
      UIImageView *imgVw = (UIImageView *)[cell viewWithTag:10]; 
      imgVw.image = image; 

     } 

    }]; 

希望這是明確的有用:)

0

dequeueReusableCellWithIdentifier需要一個UITableViewCell對象從隊列中。從隊列中取出時單元格的內容不會被清除,因此該單元格可能會顯示一個「錯誤」的圖像,直到您的下載完成並且您的UIImageView的圖像被覆蓋。嘗試設置圖像屬性爲零後離開單元格,以便在加載新數據之前不顯示任何內容。

((UIImageView *)[cell viewWithTag:10]).image = nil; 
相關問題