ios - 如何使用 Swift 和 parse.com 在 tableview 中显示图像

标签 ios image swift uitableview parse-platform

我正在尝试从 Parse 向我的单元格查询图像,然后在单击该单元格时向 View Controller 查询。在最近使用 Swift 更新之前,我确实有这个工作,现在我不确定出了什么问题。

import UIKit
import Parse
import ParseUI
import Bolts

class ProductTableViewController: PFQueryTableViewController {

    // Initialise the PFQueryTable tableview
    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!

        // Configure the PFQueryTableView
        self.parseClassName = "Products"
        self.textKey = "productName"
        self.imageKey =  "productImage"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = false

    }



    // Define the query that will provide the data for the table view
    override func queryForTable() -> PFQuery {
        let query = PFQuery(className: "Products")
        query.orderByDescending("createdAt")
        return query
    }


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

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! Customcells!
        if cell == nil {
            cell = Customcells(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }

        // Extract values from the PFObject to display in the table cell
        if let ProductName = object?["ProductName"] as? String {
            cell.ProductName.text = ProductName
        }


        // Display product image
      let initialThumbnail = UIImage(named: "question2")
        cell.productImage.image? = initialThumbnail!
        if let thumbnail = object?["ProductImage"] as? PFFile {
            cell.productImage.file = thumbnail
            cell.productImage.loadInBackground()
        }



        return cell
    }



    override func viewDidLoad() {
        super.viewDidLoad()



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using [segue destinationViewController].
        let productView = segue.destinationViewController as! ProductViewController

        // Pass the selected object to the destination view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let row = Int(indexPath.row)
            productView.currentObject = (objects?[row] as! PFObject)



        }
    }

}

这是我的一个 View Controller 的代码,上面有 tableView。图像没有显示,但其他一切都出现了。产品名称显示在正确的位置,当单击单元格时,它会将您带到 View Controller ,显示产品描述、产品名称等。唯一没有出现的是图像。知道是什么原因造成的吗?

最佳答案

尝试使用这个:

 let profileImage:PFFile = Places[indexPath.row]["Image"] as! PFFile
 profileImage.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?)-> Void in
            if (error == nil) {
                cell.pic.setImage(UIImage(data: imageData!)!, forState: UIControlState.Normal)
            }
        }

关于ios - 如何使用 Swift 和 parse.com 在 tableview 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249031/

相关文章:

swift - 如何在 Swift 2.1 中动态创建角色

ios - 如何在 Swift 3 中设置 UIButton 的图像?

ios - Xcode 提交 : Couldn't communicate with helper application

ios - 将希伯来语文本保存到 NSUserDefaults 返回奇怪的编码

javascript - iOS "Open in background"动画在 CSS/JS 中重新创建

ios - 刷新导航栏背景颜色

ios - 在 iOS 上,CALayer 位图(CGImage 对象)如何显示到显卡上?

c# - 使用 C# 将字节转换为图像时出现 "parameter is not valid"

php - 在(纯)PHP/MySQL 中查找类似图像

python - 使用 PIL 将图像转换为特定调色板而不抖动