2017-01-24 10 views
0

私はFirebase上にある画像で表示されるコレクションビューで作業しています。すべてが正常に動作しますが、私はセグエを実行しようとすると、私はこの行のために、「予期せぬオプションの値をアンラップしながら、nilを見つけ」GET:UICollectionViewセルがnilである

if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell){} 

私はSOにここに多くの作業の例を見てきましたし、どうやら彼らはすべてで正常に動作しますその行ここで

は、関連するコードの残りの部分である。また、

//grab Firebase objects in viewdidload and put them into productsArray 

    var productsArray = [ProductsModel]() 

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return productsArray.count 
} 



override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    let imageView = cell.viewWithTag(1) as! UIImageView 
    //imageView.image = imageArray[indexPath.row] 

    let getUrl = productsArray[indexPath.row].productImg 
    imageView.loadUsingCache(getUrl!) 

    imageView.layer.borderColor = UIColor.lightGray.cgColor 
    imageView.layer.borderWidth = 1 
    imageView.layer.cornerRadius = 0 

    return cell 
} 

    //NAVIGATION 
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "itemSegue", sender: nil) 

} 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if (segue.identifier == "itemSegue"){ 

     let destinationController = segue.destination as! ItemViewController 

     if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell){ 
      destinationController.getProduct = productsArray[indexPath.row] 
     } 
    } 
} 

、I重にチェックすべてが接続されており、ストーリーボードに設定されています。

ありがとうございます!次の関数で

答えて

1

、あなたがnilとしてsenderパラメータを送信します。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    performSegue(withIdentifier: "itemSegue", sender: nil) 
} 

を次に、以下の機能で、あなたは(sender as! UICollectionViewCellsenderパラメータを受け取り、それをキャストしてみてください。このパラメータは常にnilになります。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if (segue.identifier == "itemSegue"){ 
     let destinationController = segue.destination as! ItemViewController 
     if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell){ 
      destinationController.getProduct = productsArray[indexPath.row] 
     } 
    } 
} 

あなたはそれがnilにしたくない場合は、nilsenderperformSegue(withIdentifier:sender:)機能を呼び出すことはありません。有効なオブジェクトを送信します。この場合、senderのタイプはUICollectionViewCellであると思われます。したがって、performSegue機能でセルを送信してください。

編集:Nirav Dは、とにかくindexPathに変換されてから、セルを送信する理由がないことに言及しています。私たちは行って、この全体の問題を解決することができます:

performSegue(withIdentifier: "itemSegue":, sender: indexPath) 

と:説明のため

if let indexPath = sender as? IndexPath { 
    destinationController.getProduct = productsArray[indexPath.row] 
} 
+0

おかげで、私はそれを逃した信じることができません。 – Noah

+0

いいえ、送信者としてセルを送信しないでください。indexPathが必要なので、単にindexPathを送信者として送信してください。 –

+0

@ Noah1 Niravは良い点を作っています。 'indexPath'を探しているので、' indexPath'をセルの代わりに 'sender'として送ります。 –

関連する問題