我有一个用Parse的PFFiles填充的UIImage数组。
如何将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
}
答案 0 :(得分:0)
你已经拥有它:
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
}
答案 1 :(得分:0)
只需更改此行
即可cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])
到此:
cell.friendpic.image = arrayOfFriends[indexPath.row]
答案 2 :(得分:0)
arrayOfFriends[indexPath.row].getDataInBackgroundWithBlock { (data: NSData?, erreur: NSError?) -> Void in
if erreur == nil {
let image = UIImage(data: data)
cell.friendpic.image = image
}
}
这是在cellForRowAtIndexPath
中