Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用SDImageView在TableViewCell中下载URL?_Swift_Firebase_Firebase Realtime Database_Sdwebimage - Fatal编程技术网

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

Swift 如何使用SDImageView在TableViewCell中下载URL?,swift,firebase,firebase-realtime-database,sdwebimage,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.obs

我正在尝试将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!

}
var posts=[postStruct]()
重写func viewDidLoad(){
super.viewDidLoad()
让ref=Database.Database().reference().child(“Posts”)
ref.observeSingleEvent(的值为:,其中:{snapshot in
打印(快照.childrenCount)
对于snapshot.children.allObjects中的rest![DataSnapshot]{
guard let value=rest.value as?Dictionary else{continue}
guard let title=值[“title”]为字符串,否则{continue}
guard let downloadURL=value[“Download URL”]as?String else{continue}
let post=postStruct(标题:title,downloadURL:downloadURL)
self.posts.append(post)
}
self.posts=self.posts.reversed();self.tableView.reloadData()
})
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回岗位数
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带标识符:“cell”)
让imageView=单元格?.viewWithTag(200)作为!UIImageView
imageView.sd_setImage(带:URL(字符串:“downloadURL”)、占位符图像:UIImage(名为:“placeholder.png”))
设label1=单元格?.viewWithTag(1)为!UILabel
label1.text=posts[indexPath.row].title
返回单元!
}

您需要将
SDWebimage
语法更改为->

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!

}
var posts=[postStruct]()
var downloadURL:String=“”
重写func viewDidLoad(){
super.viewDidLoad()
让ref=Database.Database().reference().child(“Posts”)
ref.observeSingleEvent(的值为:,其中:{snapshot in
打印(快照.childrenCount)
对于snapshot.children.allObjects中的rest![DataSnapshot]{
guard let value=rest.value as?Dictionary else{continue}
guard let title=值[“title”]为字符串,否则{continue}
downloadURL=值[“下载URL”]为?字符串??“
let post=postStruct(标题:title,downloadURL:downloadURL)
self.posts.append(post)
}
self.posts=self.posts.reversed();self.tableView.reloadData()
})
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回岗位数
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带标识符:“cell”)
让imageView=单元格?.viewWithTag(200)作为!UIImageView
imageView.sd_setImage(带:URL(字符串:downloadURL)、占位符image:UIImage(名为:“placeholder.png”))
设label1=单元格?.viewWithTag(1)为!UILabel
label1.text=posts[indexPath.row].title
返回单元!
}

其中downloadURL是url字符串。

首先需要从数组中选择
postStruct
,然后选择
downloadURL
。使用此选项更改
覆盖func tableView(\utableview:UITableView,cellForRowAt indexath: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!
}