[英]How do I pass button action from a nested collectionView cell?
I have a MainCollectionView used for scrolling between items, inside of one of these cells I have another collectionView with cells. 我有一个MainCollectionView用于在项目之间滚动,在其中一个单元格内我有另一个带有单元格的collectionView。 In that collection view, I have a button for each cell. 在该集合视图中,我为每个单元格都有一个按钮。 My question is how do I pass action from my button to my MainCollectionView when it is tapped? 我的问题是如何在点击时将操作从我的按钮传递到我的MainCollectionView? I did create protocol for that button in the cell but I don't know how to let MainCollectionView know when my button is tapped. 我确实在单元格中为该按钮创建了协议,但我不知道如何让我的MainCollectionView知道何时点击我的按钮。 I can call action from my cell class but I think it is better to run it in Model which is my MainCollectionView. 我可以从我的单元类中调用action,但我认为最好在Model中运行它,这是我的MainCollectionView。 Below is my button protocol. 下面是我的按钮协议。
protocol ThanhCaHotTracksCellDelegate: class {
func handleSeeAllPressed()}
weak var delegate: ThanhCaHotTracksCellDelegate?
@objc func handleSeeAllButton(){
delegate?.handleSeeAllPressed()
}
LIke NSAdi said, you're on the right track, but the delegate pattern is a bit much overhead for just a single task like notifying about a button press. LIke NSAdi说,你正处于正确的轨道上,但代表模式只需要一个单独的任务,比如通知按钮按下。
I prefer using closures, because they're lightweight and helps to keep related code together. 我更喜欢使用闭包,因为它们很轻,并且有助于将相关代码保持在一起。
This is what I'm always doing in UITableView
. 这就是我在UITableView
一直在做的事情。 So this will work in UICollectionView
too. 所以这也适用于UICollectionView
。
class MyTableViewCell: UITableViewCell {
var myButtonTapAction: ((MyTableViewCell) -> Void)?
@IBAction func myButtonTapped(_ sender: Any) {
myButtonTapAction?(self)
}
}
So when I dequeue my cell and cast it to MyTableViewCell
I can set a custom action like this: 因此,当我将我的单元格出列并将其转换为MyTableViewCell
我可以设置如下自定义操作:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCellReuseIdentifier", for: indexPath) as! MyTableViewCell
cell.myButtonTapAction = { cell in
// Put your button action here
// With cell you have a strong reference to your cell, in case you need it
}
}
When you're dequeueing your UICollectionView
cell you can obtain a reference to your button by casting the cell to your cell's custom subclass. 当您将UICollectionView
单元格出列时,可以通过将单元格转换为单元格的自定义子类来获取对按钮的引用。
Then just do the following 然后只需执行以下操作
cell.button.addTarget(self, action: #selector(didTapButton(_:)), forControlEvents: .TouchUpInside)
And outside have a function: 外面有一个功能:
@objc func didTapButton(_ sender: UIButton) {
// Handle button tap
}
Downside of this is that you have no direct access to your cell. 缺点是您无法直接访问您的手机。 You could use button.superview?
你可以使用button.superview?
but it's not a good idea since your view hierarchy could change... 但这不是一个好主意,因为您的视图层次结构可能会发生变化......
You're on the right track. 你走在正确的轨道上。
Make sure MainCollectionView (or the class that contains) it implements ThanhCaHotTracksCellDelegate
protocol. 确保MainCollectionView(或包含它的类)实现ThanhCaHotTracksCellDelegate
协议。
Then assign the delegate
as self
. 然后将delegate
指定为self
。
Something like... 就像是...
class ViewController: ThanhCaHotTracksCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
subCollectionView.delegate = self
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.