简体   繁体   English

如何从嵌套的collectionView单元格传递按钮操作?

[英]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. 我更喜欢使用闭包,因为它们很轻,并且有助于将相关代码保持在一起。

Using Closures 使用闭包

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

Using direct reference 使用直接参考

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.

相关问题 如何从嵌套的collectionView单元中查询? - How do I segue from a nested collectionView cell? 如何将值从嵌入式collectionView中的选定单元格传递到另一个viewController? - How to pass value from a selected cell in embedded collectionView to another viewController? 如果我想从在代码中打开 collectionview 的 tableview 中按下单元格,我该怎么办? - How can I do if I want to press a cell from a tableview that opens a collectionview in code? 当单元格从一个 collectionView 移动到另一个时如何实现单元格动画? - How do I achieve cell animation when it moves from one collectionView to another? 如何创建从collectionview单元到另一个collectionview单元的序列? - How can I create a segue from a collectionview cell to another collectionview cell? 如何停止collectionView单元中的相机滞后? - How do I stop camera lag in a collectionView cell? 如何返回到第一个collectionview单元格项目? - How do I return back to the first collectionview cell item? 如何在旋转时为CollectionView调整特定单元格的大小? - How do I resize a specific cell for CollectionView on rotation? 如何使自定义collectionview单元可见? - How do I make a custom collectionview cell visible? 如何通过在CollectionView中选择一个单元格来更改图像 - how do I change an image by selecting a cell in CollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM