如何在Parse中获取图像,单元格名称有效但图像无法加载

时间:2015-05-11 20:25:42

标签: image swift parsing get tableview

我可以从解析中加载图像,但是它们有一个代码名称,我想在单元格中显示图像,不加载图像!

//
import UIKit
import Parse
import Bolts

class LojasViewController: UIViewController, UITableViewDelegate {
    var ParseData = [PFObject]()
    var ParseImages = [PFFile]()

    @IBOutlet weak var saloesTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Retrieving Parse Information
        var query = PFQuery(className:"Saloes")
        query.orderByAscending("nome")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println(objects!.count)

                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    self.ParseData = objects

                    println(self.ParseData.count)
                    self.saloesTableView.reloadData()
                    for object in objects {

                        println(object["nome"])
                        println(object["cidade"])
                        println(object["imagemCelula"])
                        println(object["endereco"])
                        println(object["GeoPonto"])
                        println(object["telefone"])
                        println(object["celular"])
                        println(object["texto"])
                        println(object["Imagem1"])
                        println(object["Imagem2"])
                        println(object["Imagem3"])
                        println(object["Imagem4"])
                        println(object["Imagem5"])
                        println(object["Imagem6"])
                        println(object["Imagem7"])
                        println(object["Imagem8"])
                        println(object["Imagem9"])
                        }
                    }
                } else {

                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }
        }

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

    // Esconde statusBar
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    @IBAction func voltarButton(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func loadData () {
    }

    // MARK: - Table view data source ____________________________________

    func numberOfSectionsInTableView(tableView: UITableView!) -> Int
    {
        return 1
    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
    {
        return ParseData.count
    }

    func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
    {
        let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell

            cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String

            let imagemCelula = ParseData[indexPath!.row]["imagemCelula"] as! PFFile

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用findObjectsInBackgroundWithBlock获取Parse对象。但是,一旦您拥有了图像的解析对象,您需要通过调用getDataInBackgroundWithBlock来下载实际文件。

这是你的代码:

我更新了ParseImages,它现在是一个NSData数组。

我实施了getDataInBackgroundWithBlock

我从你从Parse中检索到的图像中为imagemCelula分配了一个UIImage。

让我知道它是如何运作的

import UIKit
import Parse
import UIKit
import Parse
import Bolts

class LojasViewController: UIViewController, UITableViewDelegate {
var ParseData = [PFObject]()
var ParseImages: [NSData] = []

@IBOutlet weak var saloesTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Retrieving Parse Information
    var query = PFQuery(className:"Saloes")
    query.orderByAscending("nome")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            println(objects!.count)

            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                self.ParseData = objects

                println(self.ParseData.count)
                self.saloesTableView.reloadData()
                for object in objects {
                    object.getDataInBackgroundWithBlock({ (parseImageData, error) -> Void in
                if (error == nil) {
                    let thatImageData: NSData = parseImageData
                    ParseImages.append(thatImageData)

                        println(object["nome"])
                        println(object["cidade"])
                        println(object["imagemCelula"])
                        println(object["endereco"])
                        println(object["GeoPonto"])
                        println(object["telefone"])
                        println(object["celular"])
                        println(object["texto"])
                        println(object["Imagem1"])
                        println(object["Imagem2"])
                        println(object["Imagem3"])
                        println(object["Imagem4"])
                        println(object["Imagem5"])
                        println(object["Imagem6"])
                        println(object["Imagem7"])
                        println(object["Imagem8"])
                        println(object["Imagem9"])
} else {
println("failed to load images")
}
                        }
                    }
                } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }
    }

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

// Esconde statusBar
override func prefersStatusBarHidden() -> Bool {
    return true
}

@IBAction func voltarButton(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

func loadData () {
}

// MARK: - Table view data source ____________________________________

func numberOfSectionsInTableView(tableView: UITableView!) -> Int
{
    return 1
}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
    return ParseData.count
}

func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell?
{
    let cell:SaloesTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as! SaloesTableViewCell

        cell.nomeSalao.text = ParseData[indexPath!.row]["nome"] as? String

        let imagemCelula = UIImage(data: ParseImages[indexPath!.row])

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
}
}