2017-03-08 220 views
1

我能夠創建類似於Instagram的UICollection View feed,但我不確定如何選擇單元格並轉到更詳細的視圖控制器。這是我的主視圖控制器的樣子。 「從主視圖控制器發送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

你需要給從細胞,而不是view.Like圖像顯示賽格瑞下面 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。

+0

你是一個驚人的救星!非常感謝喲!你不知道你幫了我多少!謝謝你十億次 – juelizabeth

+0

歡迎@juelizabeth :) – sschunara

+0

其實,我很抱歉我還有一個問題。讓imageNames = post [「image」]爲?字符串實際上應該從Firebase中檢索圖像,但不是。我不認爲我應該聲明它是一個字符串。我該怎麼辦? – juelizabeth

0

這似乎是重複的。在視圖控制器之間傳遞數據的一個很好的例子可以在這裏找到:Passing Data between View Controllers

構建您的數據也很重要。如果您的Firebase數據庫收到了Post對象,則應該創建一個本地Post對象並引用該對象。在UICollectionViewCell上,可以使用選定的indexPath來獲取指定給該單元格的Post

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

相關問題