简体   繁体   中英

Fetching data from Parse.com into custom cell (swift)

I am attempting to fetch data from parse.com into my custom cell which is full of strings and images. I believe I am either retrieving my PFFile incorrectly from parse.com or I am retrieving the PFFile correctly but converting the file to UIImage improperly. The error i am receiving is going on within the loadData() function. It reads as follows: could not find an overload for 'init' that accepts the supplied arguments

Information

//Used to set custom cell
class Information {
    var partyName = ""
    var promoterName = ""
    var partyCost = ""
    var flyerImage: UIImage
    var promoterImage: UIImage

    init(partyName: String, promoterName: String, partyCost: String, flyerImage: UIImage, promoterImage: UIImage) {
        self.partyName = partyName
        self.promoterName = promoterName
        self.partyCost = partyCost
        self.flyerImage = flyerImage
        self.promoterImage = promoterImage
    }
}

Parse fetch function

func loadData() {
    var findDataParse:PFQuery = PFQuery(className: "flyerDataFetch")
    findDataParse.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if (error == nil) {
            for object in objects! {
                var eventImage0 : UIImage
                var eventImage10 : UIImage
                let userImageFile = object["partyFlyerImage"] as! PFFile
                userImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        let eventImage = UIImage(data:imageData!)
                        eventImage0 = eventImage!
                    }
                }
                let userImageFile1 = object["partyPromoterImage"] as! PFFile
                userImageFile1.getDataInBackgroundWithBlock {
                    (imageData1: NSData?, error1: NSError?) -> Void in
                    if error1 == nil {
                        let eventImage1 = UIImage(data:imageData1!)
                        eventImage10 = eventImage1!
                    }
                }
//Error below
                var party1 =  Information(partyName: (object["partyName"] as? String)!, promoterName: (object["partyPromoterName"] as? String)!,partyCost: (object["partyCost"] as? String)!, flyerImage: UIImage(data: eventImage0)!, promoterImage: UIImage(data: eventImage10)!)

                self.arrayOfParties.append(party1)
            }
        }
        self.tableView.reloadData()

    }
}

You are fetching data from Parse in background, but processing on main thread. Try this:

func loadData() {
    var findDataParse:PFQuery = PFQuery(className: "flyerDataFetch")
    findDataParse.findObjectsInBackgroundWithBlock{
    (objects: [AnyObject]?, error: NSError?) -> Void in
        if (error == nil) {
            for object in objects! {
                var eventImage0 : UIImage
                var eventImage10 : UIImage
                let userImageFile = object["partyFlyerImage"] as! PFFile
                userImageFile.getDataInBackgroundWithBlock {
                    (imageData: NSData?, error: NSError?) -> Void in
                    if error == nil {
                        let eventImage = UIImage(data:imageData!)
                        eventImage0 = eventImage!
                        let userImageFile1 = object["partyPromoterImage"] as! PFFile
                        userImageFile1.getDataInBackgroundWithBlock {
                            (imageData1: NSData?, error1: NSError?) -> Void in
                            if error1 == nil {
                                let eventImage1 = UIImage(data:imageData1!)
                                eventImage10 = eventImage1!

                                var party1 =  Information(partyName: (object["partyName"] as? String)!, promoterName: (object["partyPromoterName"] as? String)!, partyCost: (object["partyCost"] as? String)!, flyerImage: UIImage(data: eventImage0)!, promoterImage: UIImage(data: eventImage10)!)

                                self.arrayOfParties.append(party1)
                            }
                        }
                    }
                }
            }
        }
        self.tableView.reloadData()

    }
}

Your fetching your images Asynchronously and creating your Information cell Synchronously. So when you create the cell, the images are likely not loaded and your are in effect sending nil to the constructor for the cell.

In the following code you are retrieving the image data async:

userImageFile.getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in
                if error == nil {
                    let eventImage = UIImage(data:imageData!)
                    eventImage0 = eventImage!
                }
            }

So when you assign the image data to eventImage0, the call to the Information cell initializer has probably already happened.

You need to modify the code to instead of passing the image into the cell view initializer, allow you to access the UIImageview from the Information cell, so that when the background image loads complete you can simply set the loaded image into that UI/PF/ImageView.

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