简体   繁体   中英

Setting parse images to a tableView Cell

I'm trying to save images from Parse to a variable called image stored which I can then set to tableview cells. At the moment I just have the images from parse being saved to a PFImageView. But when I try to set these to Table View Cells I get an error : Cannot assign value of type 'PFImageView' to type 'UIImage?' . Really appreciate any help.

Here's my code:

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
    }

}

错误

In your code you never initialize imagestored (PFImageView) but only image (UIImage). try to change in to

cell.logo!.image = self.image

I no have idea if PFImageView has any

PFImageView.image

instances, than should be UIImage() class.

Also you should have something like this:

 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()
                            }
                        }
                    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM