【问题标题】:populate image array with parse in swift在 swift 中使用 parse 填充图像数组
【发布时间】:2015-07-30 19:51:45
【问题描述】:

您好,我是 swift 和 parse.com 的新手,我正在尝试使用已保存的 parse 图像填充我的数组,尝试使用 dispatch_async 但不知道这是如何工作的,代码如下:

//imageArray declaration in table:
var imageArray: Array<UIImage> = []


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

    var query = PFQuery(className: "ParseClass")

    query.findObjectsInBackgroundWithBlock { ( objects: [AnyObject]?, error: NSError?) -> Void in

        if(error == nil) {

            let imageObjects = objects as! [PFObject]

            for object in objects! {

                let thumbNail = object["columnInParse"] as! PFFile

                thumbNail.getDataInBackgroundWithBlock ({ (imageData: NSData?, error: NSError?) -> Void in

                    if (error == nil) {

                        if let image = UIImage(data:imageData!) {

 //here where the error appears: Cannot invoke 'dispatch_async' with an argument list of type '(dispatch_queue_t!, () -> _)' 
                           dispatch_async(dispatch_get_main_queue()) {                                     
                                cell.image.image = self.imageArray.append( image )
                            }

                        }

                    }
                })
            }
        }
        else{

            println("Error in retrieving \(error)")

        }
    }

    return cell
}

希望你们理解这段代码。

【问题讨论】:

    标签: xcode image swift parse-platform


    【解决方案1】:

    使用该调度块的正确方法如下:

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    //perform your work here
                    self.imageArray.append(image)
                    cell.image.image = imageArray[indexPath.row]
                })
    

    编译器发出此警告的另一个原因是因为您试图将图像附加到数组将其设置为单元格图像。你应该做什么如上所示。

    最后,我建议将您的查询从cellForRowAtIndexPath: 移出并放入单独的方法中,因为这是糟糕的代码设计。

    编辑:重写方法以提供良好代码设计的示例...

    var imageArray = [UIImage]()
    
    func performLookupQuery() {
        var query = PFQuery(className: "ParseClass")
        query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?)
            if error == nil {
                let imageObjects = objects as! [PFObject]
                for object in imageObjects {
                    let thumbnail = object["columnInParse"] as! PFFile
                    thumbnail.getDataInBackgroundWithBlock{(imageData: NSData?, error: NSError?)
                        if error == nil {
                            if let image = UIImage(data: imageData!) {
                                imageArray.append(image)
                                //now your imageArray has all the images in it that you're trying to display
                                //you may need to reload the TableView after this to get it to display the images
    
                            }
                        }
                    }
                }
            }
            self.tableView.reloadData()
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.image.image = nil
        cell.image.image = imageArray[indexPath.row]
        return cell
    }
    

    我没有像你应该的那样在查询方法中添加所有错误检查,我只是写它来展示你会做什么。现在您有了这个查询方法,您可以在viewDidLoad 中调用它,然后当您尝试加载表时结果将可用。

    override func viewDidLoad(animated: Bool) {
        super.viewDidLoad(animated)
        self.performLookupQuery()
    }
    

    【讨论】:

    • 而且它甚至不能正常工作,因为你不能异步返回单元格
    • 对,因此建议将他的查询代码移到此方法之外,将其返回到UIImage 的数组中,然后使用它来设置单元格的图像。
    • @Daniel 我编辑了我的答案给你一个概述(不完整,因为它没有实现你的其他方法和tableViewdelegate 的其余方法,但希望这可以帮助你理解。
    • 我应该把所有的代码都放在这里以便更好地查看代码吗?
    • 没有我给你的足以解决你最明显的问题。在 numberOfRowsInSection 确保您返回图像 array.count
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多