json - 从 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)
}
}
我一直在看的教程已经过时了,我不知道应该放什么来将图像分配给正确的表格单元格。
解决方案
我建议使用Kingfisher。它非常易于使用。
只需将 URL 添加到图像视图
let url = URL(string: "") // enter your URL string here
self.imageView.kf.setImage(with: url)
并且不要忘记从主线程更新您的 UI
DispatchQueue.main.async {
self.tableView.reloadData()
}
推荐阅读
- php - 如何截取整个桌面?
- javascript - 停止拖动事件的传播
- java - ThreadLocal set() 然后立即 get() 并不总是获取值
- single-sign-on - 无法使用 Visualize.js 使用 Keycloak SSO 令牌登录
- javascript - 禁用手动键盘输入 JavaScript/Oracle JET
- pug - 哈巴狗表单输入类型=文本属性“大小”不起作用?
- amazon-web-services - AWS Lambda 无服务器应用程序 - .Net Core 与 Asp.Net Core(多个与单个)
- python - 子类化 Flask 可插拔视图以实现可扩展功能的最佳方式
- swift - 如何在帮助类中使用 DataSnapshot 对象?
- amazon-web-services - 无论登录如何,是否都有适用于 AWS 的通用 Cognito Id?