2017-04-17 52 views
0

我有一個UICollectionViewCell,與UIButton。我有兩種不同的行爲。第一個,當用戶按下單元格時,它將繼續到didSelectITemAt;第二個,當用戶按下單元格內的UIButton時。
我的問題是關於MyCollectionViewCell銀行代碼,我不能執行SEGUE,當我寫的代碼:從UICollectionViewCell按鈕執行Segue按鈕與單元格不同Click

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

它說的錯誤:

Value of Type MyCollectionViewCell has no member performSegue.

我也不能寫prepareForSegue,它不會自動完成。

如何從單元格創建一個segue,與點擊單元格本身不同?

+0

你有沒有想過使用委託? – Valdmer

+0

所以你試圖從單元格,以及單元格內的按鈕繼續?哪一個不工作? –

+0

我既想要,又想讓他們去不同的地方。 –

回答

8

您不能從您的UICollectionViewCell子類別調用performSegue,因爲就是UICollectionViewCell那樣。

爲什麼它正在didSelectItemAtIndexPath()的原因是因爲我想你UICollectionViewdelegateUIViewController子,有什麼所謂performSegueWithIdentifier:()`功能。

當您點擊您的UICollectionViewCell中的按鈕時,您需要通知您的UIViewController,因爲您有各種可能性,例如KVO或使用委託。

這是一個小小的代碼sniplet,如何使用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) 
    } 
} 

編輯: 如果你關心,哪個小區觸摸事件happend,使用委託模式。

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) 
    } 
}