gpt4 book ai didi

ios - 将 Firebase 数据从主视图 Controller 发送到详细 View Controller

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:57 25 4
gpt4 key购买 nike

我能够创建类似于 Instagram 的 UICollection View 提要,但我不确定如何选择单元格并转到更详细的 View Controller 。这是我的主视图 Controller 的样子。 '

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {



performSegue(withIdentifier: "details", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {


if let indexPaths = self.collectionView!.indexPathsForSelectedItems{

let vc = segue.destination as! MainViewController
let indexPath = indexPaths[0] as NSIndexPath
let post = self.posts[indexPath.row] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames

print(indexPath.row)


} }}

这是我的数据库的样子:

1

    //Detailed View Controller
FIRDatabase.database().reference().child("posts").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in
if let dictionary = snapshot .value as? [String: AnyObject] {
self.BookTitle.text = dictionary["title"] as? String
self.Author.text = dictionary["Author"] as? String
self.ISBN.text = dictionary["ISBN"] as? String
self.Price.text = dictionary["Price"] as? String
self.Image.image = ["image"] as? UIImage
}
})

上面是我的详细 View Controller 。然而,当我点击单元格时,我的信息没有被传递

最佳答案

您需要从单元格而不是 View 提供转场。如下图所示 Segue from collectionviewcell

然后修改你的代码如下所示:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// No Need to call this performSegue(withIdentifier: "details", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {


if let indexPaths = self.collectionView!.indexPathsForSelectedItems{

let vc = segue.destination as! MainViewController
let cell = sender as UICollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let post = self.posts[indexPath.row] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames

print(indexPath.row)


} }}

然后在 DetailsViewController 中的代码将如下所示(无需再次引用 firebase,因为您已经拥有所有信息):

self.BookTitle.text = self.Booked
self.Author.text = self.Author
self.ISBN.text = self.ISBN
self.Price.text = self.Price
if let stringImage = self.imageNames as? String {

let imageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")

imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
if error == nil {
self.Image.image = UIImage(data: data!)


}else {
print("Error downloading image:" )
}

在viewDidLoad中编写代码。

关于ios - 将 Firebase 数据从主视图 Controller 发送到详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662810/

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