将解析图像设置为tableView单元格

时间:2016-01-27 16:42:33

标签: ios swift uitableview parse-platform

我试图将图像从Parse保存到一个名为image的变量存储,然后我可以将其设置为tableview单元格。目前我只是将解析后的图像保存到PFImageView中。但是当我尝试将它们设置为Table View Cells时,我收到一个错误:无法分配类型的值' PFImageView'输入' UIImage?' 。非常感谢任何帮助。

这是我的代码:

import UIKit
import Parse
import Bolts
import ParseUI

class YourEvents: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var currentuser = PFUser.currentUser()?.username


    //array
    var testArray = [String]()

    var testdecr = [String]()
    var image = UIImage()

     var imagestored = PFImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        var query = PFQuery(className:"Companies")
         let pUserName = PFUser.currentUser()?["username"] as? String

        query.whereKey("createdby", equalTo:"\(pUserName)")
        // let runkey = query.orderByAscending("companyname")
        query.findObjectsInBackgroundWithBlock{
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil {
                //do something with the found objects
                if let objects = objects as [PFObject]! {
                    for object in objects {

                        let load = object.objectForKey("companyname") as! String
                        self.testArray .append(load)

                        print(self.testArray)

                        let load2 = object.objectForKey("companydescription") as! String
                        self.testdecr.append(load2)

                        print(self.testdecr)

                         if let userImageFile = object["imagefile"] as? PFFile {
                         userImageFile.getDataInBackgroundWithBlock {
                            (imageData: NSData?, error: NSError?) -> Void in
                            if error == nil {
                                if let imageData = imageData {
                                     self.image = UIImage(data:imageData)!
                                }
                            }
                        }

                    }


                        }

                }
            } else {
                //log details of failure
                print("Error: \(error!) \(error?.userInfo) ")

            }


        }

        // reload  UIViewController and UITabkeView
        sleep(3)

        do_table_refresh()
    }

    func do_table_refresh () {
        dispatch_async(dispatch_get_main_queue(),  {
            self.tableView.reloadData()
            return
        })
    }

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



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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("yourstartups", forIndexPath: indexPath) as! YourStartupsCell
        cell.lbTitle!.text = self.testArray[indexPath.row]
        cell.lbDescription!.text = self.testdecr[indexPath.row]
        cell.logo!.image = self.imagestored



        return cell
    }

}

the error

2 个答案:

答案 0 :(得分:0)

在您的代码中,您永远不会初始化图像存储(PFImageView),而只能初始化图像(UIImage)。尝试改变

cell.logo!.image = self.image

我不知道PFImageView是否有任何

PFImageView.image

实例,应该是UIImage()类。

你也应该这样:

 if let userImageFile = object["imagefile"] as? PFFile {
                     userImageFile.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            if let imageData = imageData {
                                 self.image = UIImage(data:imageData)!

                                 var img = UIImage(data:imageData)
                                 image.append(img)
                                 self.tableView.reloadData()
                            }
                        }
                    }

答案 1 :(得分:-1)

尝试将图像设置为UIImage并在下一行调用do_table_refresh()