简体   繁体   English

从Parse.com提取数据到自定义单元格(Swift)

[英]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. 我试图将数据从parse.com提取到充满字符串和图像的自定义单元中。 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. 我相信我是从parse.com错误地检索了我的PFFile,或者是正确地检索了PFFile,但是将文件错误地转换为UIImage。 The error i am receiving is going on within the loadData() function. 我收到的错误是在loadData()函数中发生的。 It reads as follows: could not find an overload for 'init' that accepts the supplied arguments 内容如下: 找不到接受提供的参数的'init'的重载

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. 您正在后台从Parse中获取数据,但在主线程上进行处理。 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. 因此,当您创建单元格时,可能未加载图像,并且实际上向该单元格的构造函数发送了nil。

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. 因此,当您将图像数据分配给eventImage0时,对信息单元初始化程序的调用可能已经发生。

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. 您需要修改代码,以代替将图像传递到单元格视图初始化程序中,而允许您从“信息”单元格访问UIImageview,这样,当背景图像加载完成时,您只需将加载的图像设置到该UI / PF / ImageView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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