I have fetchData
method which fills my imagesArray
:
func fetchData(){
let imageQuery = PFUser.query()
imageQuery?.whereKey("username", containedIn: namesArray)
imageQuery!.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
print("\(object["username"] as! String)")
self.imagesArray.append(object["image"] as! PFFile)
if self.imagesArray.count == self.namesArray.count {
self.tableView.reloadData()
print("Names Order: \(self.namesArray)")
}
}
} else {
print("error: \(error?.localizedDescription)")
}
}
}
And I have tableView:cellForRowAtIndexPath
method which uses this array to fill the UIImageView
instance:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ChatsCell
cell.nameLabel.text = namesArray[indexPath.row]
if imagesArray.count == namesArray.count && self.imagesLoaded == false{
imagesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.imageView?.image = image
self.tableView.reloadData()
self.imagesLoaded = true
}
}
}
return cell
}
Actually I use if condition for waiting my array imagesArray
to be filled by query of another method and I use variable imagesLoaded
to prevent infinite call of reloadData
method. How can I write this method without if
statement?