簡體   English   中英

如何在單元格中顯示UIImage數組?

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

我有一個UIImage數組,其中填充了來自Parse的PFFiles。

如何將UIImages放入單元格中?

在此處輸入圖片說明

下面是我的代碼。

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
}

您已經擁有了:

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
}

只需更改此行

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

對此:

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

這在cellForRowAtIndexPath中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM