I'm trying to populate a CollectionView with images stored on a Parse.com account. I wrote a simple query to get these images and store them in an array. I just don't know how to go about putting those images in the cells.
class AvatarCollectionViewController: UICollectionViewController {
var avatars: [PFObject] = []
var user: PFUser!
var imageFile: PFFile
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Get the avatars from Parse
let avatarQuery = PFQuery(className: "Avatar")
avatarQuery.findObjectsInBackgroundWithBlock{
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
self.avatars.append(object)
self.imageFile = object["file"] as! PFFile
}
}
}
}
}
Further down I have the function that handles the cell. I know I'm getting close but I just can't get the images to show:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AvatarViewCell
let userImageFile = self.avatars[indexPath.row]["file"]
print(userImageFile)
userImageFile?.getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil{
let image = UIImage(data: imageData!)
cell.imageCell.image = image
} else {
print(error)
}
}
//cell.imageCell.image = UIImage(named: self.avatars[indexPath.row]["file"] as! String)
return cell
}
The whole view is black, no errors. Any help is appreciated. Thanks
EDIT: Updated cellForItemAtIndexPath function. Nil error crash at cell.imageCell.image = image
, even though print(userImageFile)
prints a value to the console.