1

I was able to create a UICollection View feed similar to Instagram but I am not sure how to select the cells and go to a more detailed view controller. Here are what my main view controller looks like. '

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)


        }  }}

Here is what my database looks like:

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

Above is my detailed view controller. However, when I click the cells, my information is not passed

AL.
  • 36,815
  • 10
  • 142
  • 281
juelizabeth
  • 485
  • 1
  • 8
  • 31
  • Hi. Please post your code snippets as text and format them accordingly instead of posting screenshots. – AL. Mar 08 '17 at 04:06
  • @AL. I have posted my code snippets as text – juelizabeth Mar 08 '17 at 04:14
  • you should parse the data from firebase in Main ViewController and pass it to detailViewController – Willjay Mar 08 '17 at 05:16
  • your code just show you parse data from firebase inside detailViewController. In other words, you have nothing to be passed to your detailViewController – Willjay Mar 08 '17 at 05:17

3 Answers3

2

You need to give segue from cell instead of view.Like shown in image below Segue from collectionviewcell

Then modify your code as shown below:

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)


        }  }}

then in DetailsViewController code will be like below (no need to reference firebase again as you already have all info) :

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

Write code in viewDidLoad.

sschunara
  • 2,285
  • 22
  • 31
  • you are an AMAZING life saver! Thank yo so much! you have no idea how much you have helped me! Thank you a billion times – juelizabeth Mar 08 '17 at 06:14
  • actually, I apologize I have one more question. let imageNames = post["image"] as? String is actually supposed to retrieve an image from firebase but it does not. I don't think I am supposed to declare it as a string. What should I do? – juelizabeth Mar 08 '17 at 06:31
  • your image must be in storage I guess, In firebase database you store just name. so using that name download that image from storage. Like storageRef.child('images/stars.jpg').getDownloadURL().then(function(url) { //you have download url here so download it and cache it, so you don't need to download every time next time}) – sschunara Mar 08 '17 at 06:33
  • I apologize again, thank you for being patient. I thought it worked, but I guess it didn't. I posted my updated code in an answer below. What am I doing wrong? – juelizabeth Mar 08 '17 at 08:19
0

This seems to be a duplicate. A great example of passing data between view controllers can be found here: Passing Data between View Controllers.

Structuring your data is also important. If you have a Post object received from your Firebase Database, you should create a local Post object and reference that. On the UICollectionViewCell, you can use the selected indexPath to get the Post designated to that cell.

Community
  • 1
  • 1
jnewkirk
  • 391
  • 1
  • 2
  • 13
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)


    })}    }  }}
juelizabeth
  • 485
  • 1
  • 8
  • 31