2016-09-01 4 views
0

UICollectionViewにカスタムセルを実装しようとしています。 セットアップが次です:私は、ダウンロードした画像のデータを取得する場合CollecitonViewが常にデータを正しく表示するとは限りません

  • 4細胞
  • =>そのイメージとセルのImageViewのを埋めます。
  • else:プレースホルダを使用します。

PFFilesの画像はimageFileDic:[String:PFFile]に保存されます。今、時々(1/5)私のアプリケーションは、二度の画像を表示することを決定、または細胞NRの位置にある

let collectionCell:SettingsCollectionViewCell = 
    collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", 
    forIndexPath: indexPath) as! SettingsCollectionViewCell 


    if indexPath.row < imageFileDic.count { 


     if let imageId = imageFileIds[indexPath.row] as? String { 

      if let imageData = imageFileDic[imageId]{ 

       collectionCell.collectionViewImage.file = imageData 
       collectionCell.collectionViewImage.loadInBackground() 

      } 

     } 
    } else{ 

     collectionCell.collectionViewButton.setImage(UIImage(), forState: .Normal) 
     collectionCell.collectionViewImage.image = UIImage(named: "CameraBild.png") 

    } 

これは私のcellForItemAtIndexPathに更新されます。 4.

私のクエリでは、私は常に新しいデータを追加する前に辞書と配列を削除しています。

アイデア?

編集: これは私が呼び出していますPFQueryです:

let imageQuery = PFQuery(className: "Images") 
    imageQuery.whereKey("sender", equalTo: objectID!) 
    imageQuery.addAscendingOrder("createdAt") 
    imageQuery.cachePolicy = .NetworkElseCache 

    imageQuery.findObjectsInBackgroundWithBlock { (objects, error) in 
     if error != nil { 
     createAlert(error!) 
     } 
     else{ 
      if let objects = objects{ 


       self.imageFileDic.removeAll() 
       self.imageFileIds.removeAll() 

       for object in objects{ 

        if let id = object.objectId{ 
         print("id found") 
     if let imageFile = object.objectForKey("imageFile") as? PFFile{ 

         self.imageFileIds.append(id) 
         self.imageFileDic[id] = imageFile 



          if object == objects.last{ 
          print(self.imageFileIds.count) 
          print(self.imageFileDic.count) 

          self.mainCollectionView.reloadData() 
          } 

         } 
        } 

       } 
      } 
     } 
    } 

} 
+0

に画像を更新するべきだと思います。私はあなたがcollectionCell.collectionViewImage.image = nillを最初からやっていないと確信しています。 –

+0

cellForItemAtIndexPath全体を投稿するだけでなく、クエリコードを投稿し、非同期応答の処理方法を説明します。 –

+0

再利用可能なセルは、セル項目の初期値を設定しないと仮定できます。 –

答えて

1

は、私はあなたが完全にyoureのcellForItemAtIndexPathメソッドを追加し、メインスレッド

dispatch_async(dispatch_get_main_queue()) { 
     collectionCell.collectionViewImage.file = imageData 
     collectionCell.collectionViewImage.loadInBackground() 
    } 
+0

これは正確にどこにある必要がありますか?私のcellForItemでは? – JVS

+0

はい、cellForItemAtIndexPath内 – Giang

+0

これはうまくいくと思いましたが、今問題が戻ってきました。 – JVS

関連する問題