0

我正在使用异步下载的图像处理 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 不起作用。

谢谢!

4

1 回答 1

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

我希望这会有所帮助,问候

于 2016-07-05T22:15:35.857 回答