【问题标题】:Send Firebase data from Main View Controller to Detailed View Controller将 Firebase 数据从主视图控制器发送到详细视图控制器
【发布时间】:2017-03-08 19:19:10
【问题描述】:

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

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)


        }  }}

这是我的数据库的样子:

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

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

【问题讨论】:

  • 嗨。请将您的代码 sn-ps 发布为文本并相应地格式化它们,而不是发布屏幕截图。
  • @AL。我已将我的代码 sn-ps 发布为文本
  • 你应该在 Main ViewController 中解析来自 firebase 的数据并将其传递给 detailViewController
  • 您的代码只是显示您从 detailViewController 中的 firebase 解析数据。换句话说,你没有任何东西可以传递给你的 detailViewController

标签: ios swift firebase firebase-realtime-database uicollectionviewcell


【解决方案1】:

您需要从单元格而不是视图中进行转场。如下图所示

然后修改你的代码如下图:

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 中编写代码。

【讨论】:

  • 你是一个了不起的救生员!非常感谢你!你不知道你帮了我多少!谢谢你十亿次
  • 实际上,我很抱歉我还有一个问题。让 imageNames = post["image"] as? String 实际上应该从 firebase 检索图像,但它没有。我认为我不应该将其声明为字符串。我该怎么办?
  • 我猜你的图像必须在存储中,在 firebase 数据库中你只存储名称。因此使用该名称从存储中下载该图像。 like storageRef.child('images/stars.jpg').getDownloadURL().then(function(url) { //你这里有下载url所以下载并缓存,这样你下次不需要每次都下载时间})
  • 再次道歉,感谢您的耐心等待。我认为它有效,但我想它没有。我在下面的答案中发布了我更新的代码。我做错了什么?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    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)
    
    
        })}    }  }}
    

    【讨论】:

    • 不要在主视图控制器中下载图像。在 DetailsviewController 中下载。 Anfd 在闭包中分配图像对象而不是外部
    • 将整个 if {} 带到详细信息并从此处删除
    • 你介意给我看看。我仍然不断收到错误
    • 我已经更新了我的答案。在 DetailsViewController 中更改。
    • 谢谢你,你救了我几天的心痛
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多