如何使用SDImageView在TableViewCell中下载URL?

时间:2017-06-13 23:46:51

标签: swift firebase firebase-realtime-database sdwebimage

我正在尝试使用带有表视图的SDImageView Cocoapod来从数据库中检索URL并将其转换为可查看的图像。当我使用下面的代码时,我不会得到图像作为回报,我的代码出了什么问题?谢谢!

var posts = [postStruct]()


override func viewDidLoad() {
    super.viewDidLoad()

    let ref = Database.database().reference().child("Posts")

    ref.observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot.childrenCount)

        for rest in snapshot.children.allObjects as! [DataSnapshot] {

            guard let value = rest.value as? Dictionary<String,Any> else { continue }
            guard let  title = value["Title"] as? String else { continue }
            guard let  downloadURL = value["Download URL"] as? String else { continue }


            let post = postStruct(title: title, downloadURL: downloadURL)

            self.posts.append(post)

        }
        self.posts = self.posts.reversed(); self.tableView.reloadData()

    })

}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    let imageView = cell?.viewWithTag(200) as! UIImageView

   imageView.sd_setImage(with: URL(string: "downloadURL"), placeholderImage: UIImage(named: "placeholder.png"))

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title
    return cell!

}

2 个答案:

答案 0 :(得分:1)

您需要将SDWebimage语法更改为 - &gt;

var posts = [postStruct]()
var  downloadURL : String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    let ref = Database.database().reference().child("Posts")

    ref.observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot.childrenCount)

        for rest in snapshot.children.allObjects as! [DataSnapshot] {

            guard let value = rest.value as? Dictionary<String,Any> else { continue }
            guard let  title = value["Title"] as? String else { continue }
              downloadURL = value["Download URL"] as? String ?? ""


            let post = postStruct(title: title, downloadURL: downloadURL)

            self.posts.append(post)

        }
        self.posts = self.posts.reversed(); self.tableView.reloadData()

    })

}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    let imageView = cell?.viewWithTag(200) as! UIImageView

    imageView.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "placeholder.png"))

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title
    return cell!

}

其中downloadURL是url String。

答案 1 :(得分:1)

首先需要从数组中选择postStruct,然后选择downloadURL。用此更改您的override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell方法。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    let imageView = cell?.viewWithTag(200) as! UIImageView

    let post = self.posts[indexPath.row];
    imageView.sd_setImage(with: URL(string: post.downloadURL), placeholderImage: UIImage(named: "placeholder"))

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title
    return cell!
}