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

时间:2017-03-08 04:04:55

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

我能够创建一个类似于Instagram的UICollection View Feed,但我不知道如何选择单元格并转到更详细的视图控制器。这是我的主视图控制器的样子。 “

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

以上是我的详细视图控制器。但是,当我单击单元格时,我的信息不会被传递

3 个答案:

答案 0 :(得分:2)

您需要从单元格而不是视图中提供segue。如下图所示 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中编写代码。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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)


    })}    }  }}