Search code examples
swiftfirebasefirebase-realtime-databasesdwebimage

How do I use SDImageView to download URL within a TableViewCell?


I am trying to use the SDImageView Cocoapod with a table view to retrieve a URL from a database and turning it into a viewable image. When I use the code bellow I don't get images in return, what is wrong with my code? Thanks!

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!

}

Solution

  • You need to change SDWebimage syntax as ->

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

    Where downloadURL is url String.