如何从嵌套的 collectionView 单元格传递按钮操作? [英] How do I pass button action from a nested collectionView cell?

查看:16
本文介绍了如何从嵌套的 collectionView 单元格传递按钮操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MainCollectionView 用于在项目之间滚动,在这些单元格中的一个内,我有另一个带有单元格的 collectionView.在该集合视图中,每个单元格都有一个按钮.我的问题是如何在点击按钮时将动作从按钮传递到 MainCollectionView?我确实为单元格中的那个按钮创建了协议,但我不知道如何让 MainCollectionView 知道我的按钮何时被点击.我可以从我的单元类中调用操作,但我认为最好在我的 MainCollectionView 模型中运行它.下面是我的按钮协议.

I have a MainCollectionView used for scrolling between items, inside of one of these cells I have another collectionView with cells. 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? 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. I can call action from my cell class but I think it is better to run it in Model which is my MainCollectionView. Below is my button protocol.

protocol ThanhCaHotTracksCellDelegate: class {
func handleSeeAllPressed()}

weak var delegate: ThanhCaHotTracksCellDelegate?


@objc func handleSeeAllButton(){
 delegate?.handleSeeAllPressed()
}

推荐答案

就像 NSAdi 所说的那样,您走在了正确的轨道上,但是对于单个任务(例如通知按下按钮)而言,委托模式的开销有点大.

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.

我更喜欢使用闭包,因为它们是轻量级的,有助于将相关代码保持在一起.

I prefer using closures, because they're lightweight and helps to keep related code together.

这就是我在 UITableView 中一直在做的事情.所以这也适用于 UICollectionView.

This is what I'm always doing in UITableView. So this will work in UICollectionView too.

class MyTableViewCell: UITableViewCell {
    var myButtonTapAction: ((MyTableViewCell) -> Void)?

    @IBAction func myButtonTapped(_ sender: Any) {
        myButtonTapAction?(self)
    }
}

因此,当我将单元格出列并将其转换为 MyTableViewCell 时,我可以设置这样的自定义操作:

So when I dequeue my cell and cast it to MyTableViewCell I can set a custom action like this:

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

使用直接引用

当您出列 UICollectionView 单元格时,您可以通过将单元格转换为单元格的自定义子类来获取对按钮的引用.

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.

然后只需执行以下操作

cell.button.addTarget(self, action: #selector(didTapButton(_:)), forControlEvents: .TouchUpInside)

和外面有一个功能:

@objc func didTapButton(_ sender: UIButton) {
    // Handle button tap
}

这样做的缺点是您无法直接访问您的手机.您可以使用 button.superview? 但这不是一个好主意,因为您的视图层次结构可能会改变...

Downside of this is that you have no direct access to your cell. You could use button.superview? but it's not a good idea since your view hierarchy could change...

这篇关于如何从嵌套的 collectionView 单元格传递按钮操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆