2016-04-23 64 views
0

我正在加載從CKA集合中從公共CloudKit數據庫中提取的圖像的tableview。但是,圖像加載順序大約兩秒,直到將正確的圖像加載到自定義UITableview單元格的UIImageView中。我知道問題在於,由於單元格是可重用的,因此圖像仍然從CloudKit下載並顯示在任何可見單元格中,而用戶在圖像視圖中顯示正確圖像之前正在滾動TableView。我想知道是否有這樣的解決方案,以便下載的圖像僅用於可見單元格而不是任何以前的單元格。CKAsset的圖片在Swift中加載失序

這裏是代碼的cellForRowAtIndexPath:提前

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PostsTableViewCell 
    cell.userInteractionEnabled = false 

    photoRecord = sharedRecords.fetchedRecords[indexPath.row] 

    cell.photoTitle.text = photoRecord.objectForKey("photoTitle") as? String 

    cell.photoImage.backgroundColor = UIColor.blackColor() 
    cell.photoImage.image = UIImage(named: "stock_image.png") 

    if let imageFileURL = imageCache.objectForKey(self.photoRecord.recordID) as? NSURL { 
     cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageFileURL)!) 
     cell.userInteractionEnabled = true 
     print("Image Cached: \(indexPath.row)") 
    } else { 

     let container = CKContainer.defaultContainer() 
     let publicDatabase = container.publicCloudDatabase 
     let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs:[self.photoRecord.recordID]) 
     fetchRecordsImageOperation.desiredKeys = ["photoImage"] 
     fetchRecordsImageOperation.queuePriority = .VeryHigh 

     fetchRecordsImageOperation.perRecordCompletionBlock = {(record:CKRecord?, recordID:CKRecordID?, error:NSError?) -> Void in 
      if let imageRecord = record { 
       NSOperationQueue.mainQueue().addOperationWithBlock() { 
        if let imageAsset = imageRecord.objectForKey("photoImage") as? CKAsset{ 
         cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!) 
         self.imageCache.setObject(imageAsset.fileURL, forKey:self.photoRecord.recordID) 
         cell.userInteractionEnabled = true 
         } 
        } 
       } 
      } 
     publicDatabase.addOperation(fetchRecordsImageOperation) 
     } 
    return cell 
} 

謝謝!

回答

1

您的表格視圖出現時和調用fetchRecordsImageOperation.perRecordCompletionBlock時有延遲。在這段時間內,用戶可以滾動表格視圖,使表格視圖單元出列,並使用不同的索引路徑和與其相關的不同數據重新排隊,如果您沒有檢查單元格的索引路徑與構建fetchRecordsImageOperation.perRecordCompletionBlock時相同,行:cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)將導致圖像被放置在已經顯示不同數據的單元中。你可以像這樣修改你的完成塊,以避免這種情況。

if let imageRecord = record { 
       NSOperationQueue.mainQueue().addOperationWithBlock() { 
        if let imageAsset = imageRecord.objectForKey("photoImage") as? CKAsset{ 
         if indexPath == tableView.indexPathForCell(cell){ 
          cell.photoImage.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!) 
         } 
         self.imageCache.setObject(imageAsset.fileURL, forKey:self.photoRecord.recordID) 
         cell.userInteractionEnabled = true 
         } 
        } 
       } 
+0

這正是我需要的!我以爲這是問題,但我不太確定在cellForRowAtIndexPath中實現解決方案的位置。再次感謝! –