0

I am trying to implement a UICollectionView with custom cells. The setup is the following:

  • 4 Cells
  • If I get the data of an downloaded image => fill the cell's imageView with that image.
  • else: use a placeholder.

The PFFiles of the images are saved within imageFileDic:[String:PFFile].

This is my UPDATED cellForItemAtIndexPath:

   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")

    }

Now sometimes (1/5 times) my application decides to display an image twice, or in the position of cell nr. 4.

in my query I am always deleting the dictionaries and arrays before appending new data.

Any ideas?

Edit: This is the PFQuery I am calling:

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()
                            }

                        }
                    }

                }
            }
        }
    }

}
JVS
  • 2,592
  • 3
  • 19
  • 31
  • add youre cellForItemAtIndexPath method fully. I bet you do not do collectionCell.collectionViewImage.image = nill in very beginning – Oleg Gordiichuk Sep 01 '16 at 11:08
  • In addition to posting your entire cellForItemAtIndexPath, also post your query code and explain how it handles async response. – Duncan C Sep 01 '16 at 11:10
  • I can assume that reusable cell do not set initial values for cell items. – Oleg Gordiichuk Sep 01 '16 at 11:11
  • @OlegGordiichuk the image is not set before I downloaded the data. Rather than that it's a blank gray space. – JVS Sep 01 '16 at 11:55
  • @DuncanC Query code is now posted. Is there a possibility of errors with NetworkElseCache? – JVS Sep 01 '16 at 11:56
  • @JVS place this code collectionCell.collectionViewImage.image = nil after let collectionCell:SettingsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! SettingsCollectionViewCell – Oleg Gordiichuk Sep 01 '16 at 11:57
  • @OlegGordiichuk tested it, but its not working. – JVS Sep 01 '16 at 12:00

1 Answers1

1

I think you should update image to main thread

dispatch_async(dispatch_get_main_queue()) { 
        collectionCell.collectionViewImage.file = imageData
        collectionCell.collectionViewImage.loadInBackground()
    }
Giang
  • 2,384
  • 2
  • 25
  • 26