gpt4 book ai didi

ios - 从 firebase 存储中检索图像以显示在 tableView 单元格上

转载 作者:行者123 更新时间:2023-11-29 00:06:23 25 4
gpt4 key购买 nike

我只需要帮助我用 firebase 构建 Instagram 克隆,我有一个问题,我无法从 firebase 存储中检索图像以显示在 tableView 单元格上,你能帮我吗:(

导入 UIKit

导入 FirebaseAuth

导入 Firebase 数据库

类 HomeViewController: UIViewController ,UITableViewDelegate {

@IBOutlet weak var tableview: UITableView!
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()

tableview.dataSource = self
loadposts()

// var post = Post(captiontxt: "test", photoUrlString: "urll")
// print(post.caption)
// print(post.photoUrl)

}

func loadposts() {
Database.database().reference().child("posts").observe(.childAdded){ (snapshot: DataSnapshot)in
print(Thread.isMainThread)
if let dict = snapshot.value as? [String: Any]{
let captiontxt = dict["caption"] as! String
let photoUrlString = dict["photoUrl"] as! String
let post = Post(captiontxt: captiontxt, photoUrlString: photoUrlString )
self.posts.append(post)
print(self.posts)
self.tableview.reloadData()
}
}
}

@IBAction func logout(_ sender: Any) {
do {
try Auth.auth().signOut()
}catch let logoutErrorr{
print(logoutErrorr)
}
let storyboard = UIStoryboard(name: "Start", bundle: nil)
let signinVC = storyboard.instantiateViewController(withIdentifier: "SigninViewController")
self.present(signinVC, animated: true, completion: nil)


}

}扩展 HomeViewController: UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 posts.count

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell
cell.captionLabel.text = posts[indexPath.row].caption
cell.postimage.image = posts[indexPath.row].photoUrl
// print(cell.captionLabel.text)
// print(cell.daysLabel.text)


return cell
}

enter code here

导入基金会类帖子{ 变种标题:字符串 var photoUrl: 字符串

init(captiontxt: String, photoUrlString: String) {
caption = captiontxt
photoUrl = photoUrlString

}

最佳答案

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell

cell.postimage.image = nil

cell.tag += 1
let tag = cell.tag

cell.captionLabel.text = posts[indexPath.row].caption

let photoUrl = posts[indexPath.row].photoUrl

getImage(url: photoUrl) { photo in
if photo != nil {
if cell.tag == tag {
DispatchQueue.main.async {
cell.postimage.image = photo
}
}
}
}

return cell
}

func getImage(url: String, completion: @escaping (UIImage?) -> ()) {
URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
if error == nil {
completion(UIImage(data: data!))
} else {
completion(nil)
}
}.resume()
}

关于ios - 从 firebase 存储中检索图像以显示在 tableView 单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47948951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com