我有一个项目对象,其关系类包含与该项目相关的多个图像。问题是我试图将这些图像添加到一个数组中,所以首先在循环内部我将它们添加到一个数组中,然后在循环之后我将它们添加到图像数组中,然后当循环完成我想重新加载表View。目前似乎是
Self.imageArray.addObject(imageDic)
在循环之前调用,因此没有任何内容添加到数组中。
我该怎么做?
var relation = itemObject.relationForKey("file") as PFRelation
var imageQuery = relation.query() as PFQuery
imageQuery.findObjectsInBackgroundWithBlock { (imageObj: [AnyObject]!, error1: NSError!) -> Void in
if error1 == nil {
var imageDic = NSMutableArray()
for obj in imageObj {
var imageObject = obj as PFObject
var thumbnail = imageObject.objectForKey("file") as PFFile
thumbnail.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
let image = UIImage(data:imageData)
imageDic.addObject(image)
}
}
}
self.imageArray?.addObject(imageDic)
println(self.imageArray?.count)
}
}
self.tableView?.reloadData()
}
答案 0 :(得分:0)
看到之后调用的块,你的self.tableView?.reloadData()会在添加任何东西之前被调用。
所以我在你的代码中做了如下命令: -
var relation = itemObject.relationForKey("file") as PFRelation
var imageQuery = relation.query() as PFQuery
imageQuery.findObjectsInBackgroundWithBlock { (imageObj: [AnyObject]!, error1: NSError!) -> Void in
if error1 == nil {
var imageDic = NSMutableArray()
for obj in imageObj {
var imageObject = obj as PFObject
var thumbnail = imageObject.objectForKey("file") as PFFile
thumbnail.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
let image = UIImage(data:imageData)
// imageDic.addObject(image) //No need of this array.
self.imageArray?.addObject(imageDic)
println(self.imageArray?.count) //simply add object into this array.
}
}
}
self.tableView?.reload()
}
}
}