0

コレクションビューのセルxibの鉛筆のようなボタンがあります。コレクションセルのボタンがトリガされないアクション

enter image description here

その後、私はこのコードを持っています。

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupsCollectionViewCellIdentifier", for: indexPath) as! GroupCollectionViewCell 

    //add action to the edit button 
    cell.editButton.tag = indexPath.row 
    cell.editButton.addTarget(self, action: #selector(GroupsCollectionViewController.editGroupAction), for: .touchUpInside) 

    return cell 
} 

//segue to edit group 
func editGroupAction() { 
    performSegue(withIdentifier: "editGroupSegue", sender: self) 
} 

いつでも編集ボタンをクリックします。何も起こっていない。私は何が欠けているのだろうか。

答えて

0

あなたがセルにクラスを割り当てると、コントローラと、ボタンをリンクしてもセグエに識別子を割り当て、またfuncはコールか

あるかどうかを確認し、この

ようセグエを使用するようにブレークポイントを使用しています ​​
+0

を働いているのiOS 10.Followingを使用している場合は、セグエはまた、識別子を持っています。 –

+0

ブレークポイントを試して、funcが呼び出されたかどうかを確認していますか? –

+0

ブレークポイントでは、関数を呼び出すことさえできませんでした。ボタンをクリックすると何も行われません。 –

1

あなたの問題を考えて、上で説明したいくつかの小さな変更を加えたデモを作成しました。
cellForItemメソッドを変更しました。私の修正版は以下の通りです。

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


    cell.editButton.tag = indexPath.row 
    cell.editButton.addTarget(self, action: #selector(editGroupAction(sender:)), for: .touchUpInside) 

    return cell 
} 


とアクションメソッドは、以下のように書かれている:UICollectionViewCellオブジェクトの宣言の
1)ウェイ:私は詳細の下に変更したコードから

func editGroupAction(sender: UIButton) { 
    print("Button \(sender.tag) Clicked") 
} 



2)UIButtonにメソッド#Selectorを割り当てる方法。最後に
3)アクションメソッドでButtonタグを印刷しました。

上記の変更で、私は適切な結果を得ています。私はconsolのコードで割り当てられているように、選択したIndexの値をボタンタグとして取得しました。私が得ている出力は以下の通りです。

Button 2 Clicked 
Button 3 Clicked 
Button 4 Clicked 

これはあなたの要件に適していると思います。

0

あなたがコード

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


      let cell = collectionView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCellClass 
cell.btnJoin.tag = (indexPath as NSIndexPath).row  
cell.btnJoin.addTarget(self, action: #selector(YourViewControllerClassName.doSomething(_:)), for: UIControlEvents.touchUpInside) 
    } 

私はすでにセルのクラスを割り当てたアクション

func doSomething(_ sender: UIButton) { 
      print("sender index",sender.tag) 
    } 
関連する問題