问题描述
我有一个 UICollectionViewCell
,带有 UIButton
。我有两个不同的行动。第一个是,当用户按下单元格时,它将使用 didSelectITemAt
进入另一个视图;第二个,当用户按下单元格内的 UIButton
时。
我的问题是在Swift Code of MyCollectionViewCell
,我无法执行segue,当我编写代码时:
I have a UICollectionViewCell
, with a UIButton
. And I have two different actions. The first one, when the user presses the cell, it will segue to another view withdidSelectITemAt
; the second one, when the users presses the UIButton
inside the cell.
My problem is that on Swift Code of MyCollectionViewCell
, I cant perform a segue, When I write the Code:
self.performSegue(withIdentifier: "toStoreFromMyDiscounts", sender: nil)
它说错误:
我也写不出来 prepareForSegue
,它不会自动完成。
I also cannot write prepareForSegue
, it doesn't auto complete.
如何从单元格创建一个segue,这是不同的单击单元格本身?
How can I create a segue from a cell, that is different from click the cell itself?
推荐答案
您无法从 performSegue
调用您的 UICollectionViewCell
子类,因为在 UICollectionViewCell
就是这样。
You can not call performSegue
from your UICollectionViewCell
subclass, because there is no interface declared on UICollectionViewCell
like that.
它工作的原因 didSelectItemAtIndexPath()
是因为我认为 UICollectionView 的href =https://developer.apple.com/reference/uikit/uicollectionviewdelegate?language=objc =noreferrer>委托是一个 UIViewController
子类,其功能名为。
The reason why it is working didSelectItemAtIndexPath()
is because i suppose the delegate of your UICollectionView
is a UIViewController
subclass, what has the function called performSegueWithIdentifier:()`.
你需要通知你的 UIViewController
按钮在您的 UICollectionViewCell
中被点击,因为您有各种可能性,例如KVO或使用委托。
You need to notify your UIViewController
when the button was clicked in your UICollectionViewCell
, for what you have various possibilities, like KVO or using delegate.
这是一个小代码片段,如何使用KVO。这个解决方案很棒,只要你不在乎,按下按钮的是哪个单元格。
Here is a little code sniplet, how to use KVO. This solution is great, as long as you do not care, in which cell was the button pressed.
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)
}
}
编辑:
如果您关心触摸事件发生在哪个单元格中,请使用委托模式。
If you care, in which cell the touch event has happend, use the delegate pattern.
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)
}
}
这篇关于从UICollectionViewCell按钮执行Segue不同于Cell Click的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!