2017-05-09 8 views
0

セレクタ内で呼び出される関数に変数を渡そうとしていますが、エラーが発生しました '引数#selectorは@objcメソッドを参照していません'どのように変数をセレクタの関数に渡すのですか?セレクタスウィフトで関数に変数を渡す方法3

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "compCell", for: indexPath) as! CompCell 


    // creating the cell 
    cell.postImage.downloadImage(from: self.winners[indexPath.row].pathToImage) 
    print(self.winners[indexPath.row].votes) 
    let num = self.winners[indexPath.row].votes! 

    cell.votesLabel.text = "\(num) votes" 
    cell.title.text = self.winners[indexPath.row].title 
    cell.postID = self.winners[indexPath.row].postID 
    cell.compID = self.winners[indexPath.row].compID 
    //tempComp = cell 


    let tap = UIGestureRecognizer(target: self, action: #selector(caller(_pressed: cell))) 
    cell.isUserInteractionEnabled = true 
    cell.addGestureRecognizer(tap) 


    return cell 
} 


func caller(_pressed: CompCell) { 
     isPressed(_pressedCell: _pressed) 
} 

func isPressed(_pressedCell: CompCell) { 
    self.selectedComp = _pressedCell 
    //prepare(for: IndividualCompSegue, sender: <#T##Any?#>) 
    performSegue(withIdentifier: "IndividualCompSegue", sender: self) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if let destination = segue.destination as? IndividualCompViewController { 
     destination.comp = self.selectedComp 
    } 
} 

答えて

1

UIGestureRecognizerのアクションで任意の値を渡すことはできません。アクションは、アクションをトリガーする1つのパラメータ(UIGestureRecognizer)をとります。

あなたの場合、ジェスチャ認識機能のviewプロパティでタップをトリガーしたcellを取得できます。

func tapHandler(_ recognizer: UITapGestureRecognizer) { 
    if let cell = recognizer.view as? Compcell { 
     isPressed(_pressedCell: cell) 
    } 
} 

あなたがタップを検出したい場合にも、あなたがUITapGestureRecognizerを作成する必要があります。代わりに選択したセルを検出するために、タップジェスチャー認識を使用しての

let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler)) 

、あなたは

を使用することができます
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = self.collectionView(collectionView, cellForItemAt: indexPath) as! CompCell 
    isPressed(_pressedCell: cell)  
} 
+0

私はそれをどのように行うのか詳しく説明できますか?すみません、私はSwiftに新しいです、ありがとう! –

+0

私のセルはCompCell型で、UICollectionViewCellは問題になるでしょうか? –

+0

申し訳ありませんが、私は 'CompCell'を' UICompCell'として間違えました。私は答えを修正しました。 – vacawama

関連する問題