簡體   English   中英

AFNetworking在UITableView中下載圖像

[英]AFNetworking downloading images in a UITableView

我有一個UITableView ,它使用AFNetworking為表中的每個單元異步下載圖像。

該下載利用過渡提供了平滑的外觀和感覺,並且除了一個小錯誤外,其他所有功能都很好用:

當我快速向下滾動時,似乎每個圖像都會開始新的下載,因此當我停止滾動時,每個單元格會閃爍幾秒鍾,顯示為該單元格“ 分配 ”的所有圖像,直到顯示正確的圖像為止(最新的一個) )。

一旦記錄不再與該單元格相關,是否有一種方法可以停止下載,或者確保顯示的圖像適合該單元格?

您可以將圖像保存到字典中,關鍵是單元格的索引路徑。 因此,在滾動時,請檢查字典中是否存在該鍵並顯示圖像。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   //Cell creation
   //Other cell operations

   NSArrary *keys = [dictImage allKeys]

  if([keys containsObject:[NSString stringWithFormat:@"%@",indexPath.row]]){
     cell.imageView.image = [dictImage objectForKey:[NSString stringWithFormat:@"%@",indexPath.row]];
  }
  else{
     //Download the image
     //When download finishes save the image into dictionary
     //Alloc init the dictImage in viewDidLoad
     //Remove the objects of dictImage when reloading with new images
     [dictImage objectForKey:[NSString stringWithFormat:@"%@",indexPath.row]];
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM