简体   繁体   English

如何在单元格中显示UIImage数组?

[英]How can I display UIImage array in cell?

I have a UIImage array populated with PFFiles from Parse. 我有一个UIImage数组,其中填充了来自Parse的PFFiles。

how do I put the UIImages into the cell? 如何将UIImages放入单元格中?

在此处输入图片说明

Below is my code. 下面是我的代码。

var arrayOfFriends = [UIImage]()

    var imagequery = PFQuery(className: "_User")
    query.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
        for object in objects!{
            let userPic = object["ProPic"] as! PFFile
            userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                if(error == nil){
                    let image = UIImage(data: imageData!)
                    self.arrayOfFriends.append(image!)
                    print(self.arrayOfFriends)
                }
                dispatch_async(dispatch_get_main_queue()) {
                    self.collectionView.reloadData()
                }
            })

        }
    }
 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView


    //this is where the error occurs(error message above)
    cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true


    return cell
}

You already have it: 您已经拥有了:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView


    //this is where the error occurs(error message above)
    // change this line
    // cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])
    cell.friendpic.image = arrayOfFriends[indexPath.row] // here, arrayOfFriends[indexPath.row] is a UIImage, you don't have to do anything more
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true


    return cell
}

Just change this line 只需更改此行

cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])

to this: 对此:

cell.friendpic.image = arrayOfFriends[indexPath.row]
   arrayOfFriends[indexPath.row].getDataInBackgroundWithBlock { (data: NSData?, erreur: NSError?) -> Void in
        if erreur == nil {
            let image = UIImage(data: data)
            cell.friendpic.image = image
        }
    }

this goes in cellForRowAtIndexPath 这在cellForRowAtIndexPath中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM