2017-10-14 5 views
1

私はコレクションビューのセルに画像を持ち、その下にボタンがあります。このボタンをクリックすると、tableviewCellを読み込み、コレクションビューの画像をロードします。これを達成するために、私は、だから私は再びcollectionviewでindexPathにアクセスする

let point = sender.convert(CGPoint.zero, to: self.collectionView) 
let myIndexPath = self.collectionView.indexPathForItem(at: point) 

self.photoThumbnail.image = self.arrayOfURLImages[(myIndexPath?.row)!] 

。これを試してみました。しかしvar photoThumbnail: UIImageView! ... ..

func SellBtnTapped(_ sender: UIButton) { 
    let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! RecipeCollectionViewCell)) 
self.photoThumbnail.image = self.arrayOfURLImages[(indexPath?.row)!] 

photoThumbnailがそうのように定義された当初はこれをしなかった。しかし、これを行うことは'Unexpectedly found nil while unwrapping an optional value'を伝えるクラッシュを与えますUnexpectedly found nil....の同じクラッシュが起こっています。どのような問題になる可能性がどのようなアイデア..?

EDIT: これはなかれnilあなたindexPathを得るためですcellForItemAtIndex...

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath as IndexPath) as! RecipeCollectionViewCell 

cell.sellButton.tag = indexPath.item 

cell.sellButton.addTarget(self,action: #selector(SellBtnTapped(_:)),for: .touchUpInside) 

return cell 
} 
+0

にあなたの行動があなたのcollectionView(_ collectionView追加になります:。またエラーがで発生している行た説明UICollectionViewメソッドのコード – iPatel

+0

を –

+0

あなたはcellForItemAtを意味... @iPatel? – bws

答えて

1

ためのコードです。

別のアプローチは、

cell.myButton.tag = indexPath.item 

のようなあなたのセルのボタンのタグを設定し、コードの下SellBtnTapped方法の使用にindexPath

を得るため

方法collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPathであります

セルの画像を使用すると、画像オブジェクトが表示されるか、self.arrayOfURLImagesを使用して正しい画像を取得できます。あなたのさらなるものをしてください。

+0

これはまだクラッシュしています...これは私がしたことでした... let indexPath = NSIndexPath(item:sender .tag、セクション:0) 自己.photoThumbnail.image = self.arrayOfURLImages [indexPath.row] – bws

+0

私のコードをcellForItemAtメソッドで設定しましたか?このメソッドのコードを提供してください – iPatel

+0

質問を編集してcellForItemAtのコードを追加しました。 – bws

1

私はタグを完全に避けることをお勧めします。私はこれを少し前に書きましたが、まだそれが有用であると感じています。

extension UIView { 

    var superCollectionViewCell: UICollectionViewCell? { 
     if let cell = self as? UICollectionViewCell { 
      return cell 
     } else { 
      return superview?.superCollectionViewCell 
     } 
    } 

    var superCollectionView: UICollectionView? { 
     if let collectionView = self as? UICollectionView { 
      return collectionView 
     } else { 
      return superview?.superCollectionView 
     } 
    } 

    var indexPathOfSuperCollectionViewCell: IndexPath? { 
     guard let cell = superCollectionViewCell, let collectionView = superCollectionView else { return nil } 

     return collectionView.indexPath(for: cell) 
    } 

} 

これは

func SellBtnTapped(_ sender: UIButton) { 
    guard let indexPath = sender.indexPathOfSuperCollectionViewCell else { 
     print("button has no index path") 
     return 
    } 

    self.photoThumbnail.image = self.arrayOfURLImages[indexPath.row] 
} 
+0

タグなしのソリューション、@ Jeffery Thomasが登場しました...しかし答えに感謝します... upvoteを与えました... :) – bws

関連する問題