Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将Firebase数据从主视图控制器发送到详细视图控制器_Ios_Swift_Firebase_Firebase Realtime Database_Uicollectionviewcell - Fatal编程技术网

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

Ios 将Firebase数据从主视图控制器发送到详细视图控制器,ios,swift,firebase,firebase-realtime-database,uicollectionviewcell,Ios,Swift,Firebase,Firebase Realtime Database,Uicollectionviewcell,我可以创建一个类似Instagram的UICollection视图提要,但我不知道如何选择单元格并转到更详细的视图控制器。以下是我的主视图控制器的外观。” func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { performSegue(withIdentifier: "details", sender: self) } override

我可以创建一个类似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  
        }
    })  


上面是我的详细视图控制器。但是,当我单击单元格时,我的信息不会被传递,这似乎是重复的。在这里可以找到在视图控制器之间传递数据的一个很好的示例:


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

您需要从单元格而不是视图中指定segue。如下图所示

然后修改代码,如下所示:

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)


        }  }}
然后详细地说,IEWController代码如下所示(无需再次引用firebase,因为您已经拥有所有信息):


在viewDidLoad中编写代码

嗨。请将代码段以文本形式发布,并相应格式化,而不是发布屏幕截图。@AL.我已将代码段以文本形式发布。您应该在主ViewController中解析firebase中的数据,并将其传递给detailViewController。您的代码仅显示您在detailViewController中解析firebase中的数据。换句话说,你没有任何东西可以传递给你的DetailViewController你是一个了不起的救生员!非常感谢你!你不知道你帮了我多少忙!非常感谢你事实上,我很抱歉我还有一个问题。让imageNames=post[“image”]作为?字符串实际上应该从firebase检索一个图像,但它没有。我不认为我应该把它声明为字符串。我该怎么办?我猜你们的图像一定在存储器中,在firebase数据库中你们只存储了你们的名字。因此,使用该名称从存储器下载该图像。像storageRef.child('images/stars.jpg').getDownloadURL()。然后(函数(url){//您在这里有下载url,所以下载并缓存它,所以您不需要下次每次都下载。)我再次道歉,感谢您的耐心。我以为它管用,但我想它没用。我在下面的回答中发布了我的更新代码。我做错了什么?不要在mainviewcontroller中下载图像。详细下载iewcontroller。Anfd在闭包中指定图像对象,不要在外面。如果{}要详细说明,请将整个图像对象从Herdo中删除。您介意的话,请给我看一下。我仍然不断出错,我已经更新了我的答案。更改了详细信息查看控制器。谢谢你,你刚刚救了我几天的心痛
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)


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


    })}    }  }}