gpt4 book ai didi

ios - (iOS + Firebase) 无法将图像从 UITableViewCell 传递到下一个 ViewController

转载 作者:行者123 更新时间:2023-11-28 11:41:14 25 4
gpt4 key购买 nike

我有一个 UITableView,其中数据来自 Firebase RealtimeDatabase。一旦用户选择了行,行中的数据,即:标题、描述和图像将被带到下一个 ViewController。

我可以传递标题和描述,但无法传递图像。

这是我的 UITableView 代码:

import UIKit
import Firebase

class PostTable: UIViewController, UITableViewDelegate, UITableViewDataSource {

var tableView:UITableView!

var posts = [Post]()

override func viewDidLoad() {
super.viewDidLoad()

tableView = UITableView(frame: view.bounds, style: .plain)
view.addSubview(tableView)

let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
var layoutGuide:UILayoutGuide!

layoutGuide = view.safeAreaLayoutGuide

tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.reloadData()


observePosts()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func observePosts() {
let postsRef = Database.database().reference().child("Data")
print(postsRef)
postsRef.observe(.value, with: { snapshot in
var tempPosts = [Post]()

for child in snapshot.children{

if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let title = dict["title"] as? String,
let logoImage = dict["image"] as? String,
let url = URL(string:logoImage),
let description = dict["description"] as? String{


let userProfile = UserProfile(title: title, photoURL: url)
let post = Post(id: childSnapshot.key, title: userProfile, description: description, image: userProfile)
print(post)
tempPosts.append(post)
}
}

self.posts = tempPosts
self.tableView.reloadData()
})
}

func getImage(url: String, completion: @escaping (UIImage?) -> ()) {
URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
if error == nil {
completion(UIImage(data: data!))
} else {
completion(nil)
}
}.resume()
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(posts.count)
return posts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.set(post: posts[indexPath.row])
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let postsInfo = posts[indexPath.row]
print(postsInfo)

let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "PostTableDetailed") as! PostTableDetailed
DvC.getName = postsInfo.title.title
DvC.getDesc = postsInfo.description
// DvC.getImg = postsInfo.title.photoURL
self.navigationController?.pushViewController(DvC, animated: true)
}
}

这是第二个具有帖子详细信息的 ViewControler:

import UIKit

class PostTableDetailed: UIViewController {

var getName = String()
var getDesc = String()

@IBOutlet weak var Name: UILabel!
@IBOutlet weak var Description: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

Name.text! = getName
Description.text! = getDesc
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

我还有一些模型(Post、UserProfile)和服务(UserService 和 ImageService),如果需要解决这个问题,请告诉我。

最佳答案

如果您有 imageUrl,您只需将它从 PostTable 传递到 PostTableDetailed 并下载图像。

   // PostTable
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let postsInfo = posts[indexPath.row]
print(postsInfo)

let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "PostTableDetailed") as! PostTableDetailed
DvC.getName = postsInfo.title.title
DvC.getDesc = postsInfo.description
DvC.getImg = postsInfo.photoURL
self.navigationController?.pushViewController(DvC, animated: true)
}

// PostTableDetailed
class PostTableDetailed: UIViewController {

var getName = String()
var getDesc = String()
var imageUrl = ""

@IBOutlet weak var Name: UILabel!
@IBOutlet weak var Description: UILabel!
@IBOutlet weak var imageView: UIImageView!


override func viewDidLoad() {
super.viewDidLoad()

Name.text! = getName
Description.text! = getDesc
updayeImage()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

private func updateImage() {
URLSession.shared.dataTask(with: URL(string: self.imageUrl)!) { data, response, error in
if error == nil, let data = data {
imageView.image = UIImage(data: data)
}
}.resume()
}

}

图像将在任务完成时显示。所以我建议您向 imageView 添加一个微调器。

关于ios - (iOS + Firebase) 无法将图像从 UITableViewCell 传递到下一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53628059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com