【问题标题】:Make table images database images from JSON urls从 JSON url 制作表格图像数据库图像
【发布时间】:2020-10-10 05:43:28
【问题描述】:

我正在尝试将表格图像制作为存储在我的 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)
   }
}

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

【问题讨论】:

  • 这种方法会有问题。你能想象,作为一个用户,当它在等待数以百计的图像在滚动时下载时,当它结结巴巴并停止时滚动浏览一个 tableView?啊。更好的方法是将图像(缩略图)存储在 tableView 数据源 users 中,因此在填充单元格时,获取文本就像你是 cell.textLabel?.text = user.name 和图像 cell.imageView?.image = user.image。它速度很快,如果需要重新创建单元格,则无需重新下载已下载的数据。
  • 哦 - 你作为旁注。在您的搜索栏中,用户名不需要单独的数组self.data,因为它已经存储在数据源self.users 中。在排序、选择等时导航两个数组会变得很复杂。
  • 我无法让 users 数组调用名称,因此我必须仅使用用户名称附加数据。有什么建议吗?
  • 使用 Swift 实际上很简单,但我不知道你是什么User 类。此外,该答案与问题无关,因此有点偏离主题。对快速过滤对象数组进行搜索,您将获得数十种解决方案。如果没有,请发布一个单独的问题。

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


【解决方案1】:

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

只需将 URL 添加到图像视图中

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

别忘了从主线程更新你的用户界面

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 2017-01-15
    • 2020-05-01
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多