首页 > 解决方案 > 从 JSON url 制作表格图像数据库图像

问题描述

我正在尝试使表格图像成为存储在我的 firebase 中的特定人员的个人资料图像。我有一个可以打印 url 列表的 NSURL,但我不知道如何将它添加到每个人在自己单元格中的 UIImage 中。我这里有代码:

      import UIKit
      import Firebase

   class NetworkTableViewController: UITableViewController, UISearchBarDelegate {

var data = [String]()

var users = [User]()

let cellId = "cellId"

var filteredData: [String]!

@IBOutlet weak var searchBar: UISearchBar!

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.delegate = self

    fetchUsers()

    filteredData = data

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows

    return filteredData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId)! as UITableViewCell

    let user = users[indexPath.row]

    cell.textLabel?.text = user.name

    cell.imageView?.image = UIImage(named: "Placeholder Photo")

    if let profileImageUrl = user.profileImageUrl {
        let url = NSURL(string: profileImageUrl)

        }

    return cell
}

//Mark: Search Bar Config
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filteredData = []


    if searchText == "" {
        filteredData = data

    }
    else {
        for fruit in data {

            if fruit.lowercased().contains(searchText.lowercased()) {

                filteredData.append(fruit)
            }
        }
    }
    self.tableView.reloadData()
}

func fetchUsers() {

    Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in

        if let dictionary = snapshot.value as? [String: String] {

            let user = User()


            user.name = dictionary["name"]
            user.email = dictionary["email"]
            user.facebookUrl = dictionary["facebookUrl"]
            user.instagramUrl = dictionary["instagramUrl"]
            user.linkedInUrl = dictionary["linkedInUrl"]
            user.profileImageUrl = dictionary["profileImageUrl"]
            user.twitterUrl = dictionary["twitterUrl"]

           // print(user.name!)

            self.users.append(user)
            self.data.append(user.name!)

            self.tableView.reloadData()

        }
    }, withCancel: nil)
   }
}

我一直在看的教程已经过时了,我不知道应该放什么来将图像分配给正确的表格单元格。

标签: jsonswiftfirebasefirebase-realtime-databasereference

解决方案


我建议使用Kingfisher。它非常易于使用。

只需将 URL 添加到图像视图

let url = URL(string: "") // enter your URL string here
self.imageView.kf.setImage(with: url)

并且不要忘记从主线程更新您的 UI

DispatchQueue.main.async {
  self.tableView.reloadData()
}

推荐阅读