Cell.imageView not showing image

I have a tableView where I am trying to show an image in a cell.imageView but no matter what I do it will not show the image. an imageViewView subclass is a PFImageView. I have verified that PFFile and UIImage are not null. What am I doing wrong?

this is what i have tried so far:

conversion to UIImage

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PFTableViewCell

    cell.textLabel?.text = object.objectForKey("title") as NSString
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd" // superset of OP format
    let str = dateFormatter.stringFromDate(object.createdAt)
    cell.detailTextLabel?.text = str
    var theFile:PFFile = object.objectForKey("image") as PFFile

    theFile.getDataInBackgroundWithBlock {
        (imageData: NSData!, error: NSError!) -> Void in
        if error == nil {
            let image = UIImage(data:imageData)
            dispatch_async(dispatch_get_main_queue()) {
                cell.imageView?.image = image


    return cell



source to share

1 answer

You are setting an image from the closure, which most likely does not work on the main thread. You should wrap your code in dispatch_async


dispatch_async(dispatch_get_main_queue()) {
    cell.imageView?.image = image


I think this is a problem, but of course I cannot test it because I do not have all your sources at my disposal. Anyway, even if that doesn't fix the problem, you need to do something because the UI components need to be updated from the main thread.



All Articles