在点击/点按行之前,图片不会显示

时间:2016-07-05 07:53:01

标签: ios swift eureka-forms

我正在使用异步下载的图像处理ButtonRows,这是我的代码:

 futurSection <<< ButtonRow("aaa") {
            $0.title = ""
            }.cellUpdate { cell, row in
                cell.imageView?.imageFromServerURL("http://example.com/0x0ss2.jpg")
                row.reload()
            }.cellSetup({ (cell, row) -> () in
                row.cell.height = {
                    return 150
                }
            })

我使用了UIImageView的扩展名:

extension UIImageView {
    public func imageFromServerURL(urlString: String) {

        NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { (data, response, error) -> Void in

            if error != nil {
                print(error)
                return
            }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let image = UIImage(data: data!)
                self.image = image
                self.layoutSubviews()
                if let myViewController = self.parentViewController as? ShowDetails {
                    myViewController.form.rowByTag("aaa")?.updateCell()
                }

            })

        }).resume()
    }}

但是直到我点击行本身才会看到行中的图像。我甚至调用了updateCell并重新加载但是id不起作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个,将此代码中的代码替换为

extension UIImageView {


    public func imageFromServerURL(urlString: String,closure:((loaded : Bool) -> Void)?) {

        NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlString)!, completionHandler: { (data, response, error) -> Void in

            if error != nil {
                print(error)
                return
            }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                let image = UIImage(data: data!)
                self.image = image
                if(closure != nil)
                {
                    closure!(loaded: true)
                }
            })

        }).resume()
    }

}

然后使用此代码重新加载单元格

<<< ButtonRow("aaa") {
                $0.title = ""
                }.cellUpdate { cell, row in
                    cell.imageView?.imageFromServerURL("http://example.com/0x0ss2.jpg", closure: { (loaded) in
                        row.reload()
                    })
                }.cellSetup({ (cell, row) -> () in
                    row.cell.height = {
                        return 150
                    }
                })

我希望这有帮助,尊重