从主视图控制器发送Firebase数据到详细视图控制器

从主视图控制器发送Firebase数据到详细视图控制器

问题描述:

我能够创建类似于Instagram的UICollection View feed,但我不确定如何选择单元格并转到更详细的视图控制器。这是我的主视图控制器的样子。 “从主视图控制器发送Firebase数据到详细视图控制器

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 
     } 
    }) 

以上就是我的详细视图控制器。但是,当单击单元格时,我的信息不通过

+0

嗨。请将您的代码片段作为文本发布并相应地设置格式,而不是张贴屏幕截图。 –

+0

@AL。我已经发布了我的代码片段作为文本 – juelizabeth

+0

您应该解析来自主ViewController中的Firebase的数据并将其传递给detailViewController – WeiJay

你需要给从细胞,而不是view.Like图像显示赛格瑞下面 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。

+0

你是一个惊人的救星!非常感谢哟!你不知道你帮了我多少!谢谢你十亿次 – juelizabeth

+0

欢迎@juelizabeth :) – sschunara

+0

其实,我很抱歉我还有一个问题。让imageNames = post [“image”]为?字符串实际上应该从Firebase中检索图像,但不是。我不认为我应该声明它是一个字符串。我该怎么办? – juelizabeth

这似乎是重复的。在视图控制器之间传递数据的一个很好的例子可以在这里找到:Passing Data between View Controllers

构建您的数据也很重要。如果您的Firebase数据库收到了Post对象,则应该创建一个本地Post对象并引用该对象。在UICollectionViewCell上,可以使用选定的indexPath来获取指定给该单元格的Post

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



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!.indexPath(for: 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 
      if let imageNames = post["image"] as? String { 

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

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


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


     vc.Booked = Booked 
     vc.Authors = Authors 
     vc.ISBNS = ISBNS 
     vc.Prices = Prices 
     vc.imageNames = imageNames 

      print(indexPath?.row) 


    })} } }} 
+0

请勿在mainviewcontroller下载图像。在DetailsviewController中下载。 Anfd分配图像对象在未关闭外 – sschunara

+0

这整个就拿如果{}细节,从这里 – sschunara

+0

删除你不介意请给我看。我仍然不断收到错误 – juelizabeth