cell.imageView未显示图像

时间:2014-09-28 08:42:24

标签: ios objective-c swift uiimage parse-platform

我有一个tableView,我试图在cell.imageView中显示图像,但无论我做什么它都不会显示图像。 imageView的子类是PFImageView。我已经检查过PFFile和UIImage不是零。我做错了什么?

这是我迄今为止所尝试过的:

转换为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's 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
}

1 个答案:

答案 0 :(得分:2)

您正在从闭包中设置图像,该闭包很可能未在主线程中运行。您应该将代码括在dispatch_async

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

我认为这是问题所在,但我当然无法对其进行测试,因为我没有掌握所有资源。无论哪种方式,即使没有解决问题,也必须要做的事情是因为必须从主线程更新UI组件。