根据JSON网址制作表格图片数据库图片

时间:2020-06-19 22:13:17

标签: json swift firebase firebase-realtime-database reference

我正在尝试使表格图像成为存储在我的Firebase中的特定人的个人资料图像。我有一个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)
   }
}

我一直在观看的教程已经过时,我不知道该将图像分配给正确的表格单元格该放在哪里。

1 个答案:

答案 0 :(得分:0)

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

只需将URL添加到图像视图

<div class="black"></div>
<div class="e-with-fixed-bg">
  <div class="bg-wrap">
     <div class="bg"></div>
  </div>
  <div class="e-container">
    <h1>This works well enought</h1>
  </div>
</div>
<div class="black"></div>

别忘了还要从主线程更新用户界面

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