【问题标题】:Retrieve image from firebase storage to show on tableView cell从 Firebase 存储中检索图像以显示在 tableView 单元格上
【发布时间】:2018-06-05 13:07:38
【问题描述】:

我只需要帮助我使用 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 标题:字符串 var photoUrl: 字符串

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

}

}

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:
    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()
    }
    

    【讨论】:

    • XCode 9 有一个新的运行时主线程检查器,它检测从后台线程对 UIKit 的调用并生成警告。它旨在生成警告而不会使应用程序崩溃,但您可以尝试为您的测试目标禁用主线程检查器,或者在当前情况下忽略它(因为在以前的 Xcode 版本中它不是错误)。主线程检查器会在后台线程中找到所有与视图连接的代码并显示错误,但在这种情况下 cell.tag 不会产生任何不良后果,所以不用担心)
    • 如果要禁用主线程检查器,请执行以下操作:选择目标 -> 编辑方案 -> 测试 -> 诊断 -> 取消选中主线程检查器
    • 问题不是主线程检查器,项目运行正常,但布局没有按预期运行
    • 布局到底有什么问题?在屏幕截图上,我看到 3 行,是的,它看起来很奇怪,但这是表格设置和 PostCellTableViewCell 布局的问题。
    • 我的代码只是实现了检索图像和设置 cell.postimage 的图像属性的正确逻辑。但是postimage的外观是在PostCellTableViewCel中确定的。
    猜你喜欢
    • 2021-05-20
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2021-07-07
    • 2020-10-23
    相关资源
    最近更新 更多