2017-03-08 15 views
1

Instagramと同様のUICollection Viewフィードを作成することができましたが、セルを選択してより詳細なビューコントローラに移動する方法がわかりません。私のメインビューコントローラーの外観は次のとおりです。 「ここメインビューコントローラから詳細ビューコントローラへ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

答えて

2

あなたが次に Segue from collectionviewcell

コードを変更下の画像に示されている代わりにview.Likeのセルからセグエを与える必要がすでにすべての情報を持っているので、再度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

0

これは重複しているようです。 View Controller間でデータを渡す大きな例は、Passing Data between View Controllersです。

データの構造化も重要です。 FirebaseデータベースからPostオブジェクトを受け取った場合は、ローカルのPostオブジェクトを作成して参照する必要があります。 UICollectionViewCellでは、選択したindexPathを使用して、そのセルに指定されたPostを取得できます。

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コードで(下記のようになります。下記のように

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) 


    })} } }} 
+0

mainviewcontrollerに画像をダウンロードしないでください。 DetailsviewControllerでダウンロードしてください。 Anfdが外に出ていない閉鎖の画像オブジェクトを割り当てます – sschunara

+0

{}の場合は詳細を記入し、ここから削除してください – sschunara

+0

私に見せてください。私はまだエラーが続いている – juelizabeth

関連する問題