2017-04-17 8 views
0

私はUICollectionViewCellを持っていて、UIButtonです。私は2つの異なる行動をとっています。最初のものは、ユーザーがセルを押すと、didSelectITemAtで別のビューにセグメントします。ユーザーがセル内のUIButtonを押すと、2番目のセルが表示されます。
私の問題は、私はコードを書く場合MyCollectionViewCellのスウィフトコードの上に、私は、セグエを行う傾けることです:セルのクリックと異なるUICollectionViewCellボタンからSegueを実行

self.performSegue(withIdentifier: "toStoreFromMyDiscounts", sender: nil) 

それがエラーを言う:

Value of Type MyCollectionViewCell has no member performSegue.

私も、それをprepareForSegueを書き込むことはできません自動完了しません。

セルからセグを作成するにはどうすればできますか?それはセル自体をクリックするのと異なりますか?

+0

は、デリゲートを使用して考えたことはありますか? – Valdmer

+0

セル内のボタンからだけでなく、セルからセグをしようとしていますか?どちらがうまくいきませんか? –

+0

私は両方が欲しいです。私は彼らに別の場所に行きたいです。 –

答えて

8

no interface declaredはそのようUICollectionViewCellにありますので、あなたは、あなたのUICollectionViewCellサブクラスからperformSegueを呼び出すことはできません。

私はあなたのUICollectionViewdelegateperformSegueWithIdentifier:()`と呼ばれる機能を持っているものUIViewControllerサブクラス、だとしているため、それはdidSelectItemAtIndexPath()に取り組んでいる理由はあります。

UICollectionViewCellのボタンをクリックしたときに、UIViewControllerに通知する必要があります.KVOや代理人のようなさまざまな可能性があります。

ここでは、KVOの使い方を簡単に解説しています。あなたが気にしない限り、どのセルがボタンが押されたかで、この解決策は素晴らしいです。

import UIKit 

class CollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var button: UIButton! 
} 

class CollectionViewController: UIViewController { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CollectionViewController: UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell: CollectionViewCell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 
     // Add your `UIViewController` subclass, `CollectionViewController`, as the target of the button 
     // Check out the documentation of addTarget(:) https://developer.apple.com/reference/uikit/uicontrol/1618259-addtarget 
     cell.button.addTarget(self, action: #selector(buttonTappedInCollectionViewCell), for: .touchUpInside) 
     return cell 
    } 

    func buttonTappedInCollectionViewCell(sender: UIButton) { 
     self.performSegue(withIdentifier: "toStoreFromMyDiscounts", sender: nil) 
    } 
} 

EDIT: あなたが気にしている場合、タッチイベントが起こったセルでは、デリゲートのパターンを使用しています。

import UIKit 

protocol CollectionViewCellDelegate: class { 
    // Declare a delegate function holding a reference to `UICollectionViewCell` instance 
    func collectionViewCell(_ cell: UICollectionViewCell, buttonTapped: UIButton) 
} 

class CollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var button: UIButton! 
    // Add a delegate property to your UICollectionViewCell subclass 
    weak var delegate: CollectionViewCellDelegate? 

    @IBAction func buttonTapped(sender: UIButton) { 
     // Add the resposibility of detecting the button touch to the cell, and call the delegate when it is tapped adding `self` as the `UICollectionViewCell` 
     self.delegate?.collectionViewCell(self, buttonTapped: button) 
    } 
} 

class CollectionViewController: UIViewController { 
    @IBOutlet weak var collectionView: UICollectionView! 
} 

extension CollectionViewController: UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell: CollectionViewCell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 
     // Asssign the delegate to the viewController 
     cell.delegate = self 
     return cell 
    } 
} 

// Make `CollectionViewController` confrom to the delegate 
extension CollectionViewController: CollectionViewCellDelegate { 
    func collectionViewCell(_ cell: UICollectionViewCell, buttonTapped: UIButton) { 
     // You have the cell where the touch event happend, you can get the indexPath like the below 
     let indexPath = self.collectionView.indexPath(for: cell) 
     // Call `performSegue` 
     self.performSegue(withIdentifier: "toStoreFromMyDiscounts", sender: nil) 
    } 
} 
+0

ありがとう、私はケア魔法使いの細胞がタップされました、どうすればそれを行うことができますか? –

+1

私は自分の答えを編集しました。 – dirtydanee

+0

ありがとうございます、今すぐお試しください! –

0

も魔法のように動作別の解決策は:

extension YOURViewController : UICollectionViewDataSource 
{ 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOURCell", for: indexPath) as! YOURCollectionViewCell 

     cell.butTapped = { 
      [weak self] (YOURCollectionViewCell) -> Void in 
      // do your actions when button tapped 
     } 
    }   
    return cell 
} 


class YOURCollectionViewCell: UICollectionViewCell 
{ 
    var butQRTapped: ((YOURCollectionViewCell) -> Void)? 
    @IBAction func deleteButtonTapped(_ sender: AnyObject) { 
     butTapped?(self) 
    } 
} 
関連する問題