2017-06-08 31 views
0

我對如何將DownloadURL作爲鏈接並將其變成圖像並將其添加到我的tableview單元格中感到困惑?我在故事板中有一個imageview,不知道如何將它連接到tableview單元。謝謝!如何從URL中添加圖像到一個TableView中

override func viewDidLoad() { super.viewDidLoad() 

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

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

     print(snapshot.childrenCount) 

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

      guard let value = rest.value as? Dictionary<String,Any> else { continue } 

      guard let downloadURL = value["DownloadURL"] as? String else { continue } 

      let post = postStruct(title: title, date: date, author: author, article: article, 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 label1 = cell?.viewWithTag(1) as! UILabel 
    label1.text = posts[indexPath.row].title 
    return cell! 
} 
+0

定義YOUT細胞的一類,並添加一個UIImageView作爲出口,然後在的cellForRowAtIndexPath使用圖像管理器,例如Alamofire或SDWebImage如果我的回答幫助加載圖像 –

+0

,將其標記請(按jackdaw)讓其他人知道它是正確的 –

回答

2

谷歌這個cocoapod:SDWebImage

利用這一點,這是不可思議的

斯威夫特:

import SDWebImage 

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

在關於你的第二個問題,連接您的圖像視圖,您需要創建一個自定義單元格

+0

再次幫助我https://stackoverflow.com/questions/44530922/sdwebimage-doesnt-work – Riccardo

0

我認爲你做的錯誤的方式從firebase

  1. 下載圖像從firebase storage下載圖像的URL。您需要定義裏面的TableCell

`

let imagesRef = FIRStorage.storage().reference() 
let imageRef = self.imagesRef.child("imageName.jpg") 
imageRef.downloadURL { url, error in 
    if let error = error { 
    } else { 
     DispatchQueue.main.async { 
      cell.imageView.kf.setImage(with: url) 
     } 
    } 
} 

`

一個ImageView的
  • 您的代碼有問題,IDK的爲什麼你嘗試使用firebase database下載圖像,因爲你不能。
  • +0

    他正在使用Firebase數據庫下載圖像的URL。不是圖像。 –

    1

    1)您需要創建具有圖像的自定義單元格類,如下所示:

    class MyCustomCell: UITableViewCell { 
        //...some vars? 
        @IBOutlet myImageView: UIImageView! 
        //...some vars? 
    } 
    

    2)爲您設置table view。你可以找到點和在谷歌,SO等實施例很多教程的:Add custom cells for tableView

    3)安裝KingFisher用於高速緩存的圖像。 Kingfisher。它會讓你的tableview有平滑的滾動。(注:你可以像使用SDWebImage任何模擬)

    要安裝:

    1)pod 'Kingfisher', '~> 3.0'在POD文件

    2)pod install在終端

    3)import Kingfisher在迅速 - 代碼文件

    4)在您的table view中使用它:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! MyCustomCell 
        let row = indexPath.row 
    
        // ...other vars? 
    
        let imageURL = URL(string: self.posts[row].downloadURL) 
        cell.myImageView.kf.setImage(with: imageURL) // Using kingfisher for caching and viewing. 
        // next time, when you will use image with this URL, it will be taken from cache. 
    
        // ...other vars? 
    
        return cell 
    } 
    

    希望它可以幫助

    +0

    你能幫助我嗎? https://stackoverflow.com/questions/44530922/sdwebimage-doesnt-work – Riccardo

    +0

    @Riccardo你可以問金斯利先生的幫助:) –

    相關問題