集合视图单元格按钮不触发动作

Chr*_*sen 9 xcode uibutton uicollectionview uicollectionviewcell swift

所以我在集合视图单元格 xib 中有一个看起来像铅笔的按钮。

在此处输入图片说明

然后我有这个代码。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupsCollectionViewCellIdentifier", for: indexPath) as! GroupCollectionViewCell

    //add action to the edit button
    cell.editButton.tag = indexPath.row
    cell.editButton.addTarget(self, action: #selector(GroupsCollectionViewController.editGroupAction), for: .touchUpInside)

    return cell
}

//segue to edit group
func editGroupAction() {
    performSegue(withIdentifier: "editGroupSegue", sender: self)
}
Run Code Online (Sandbox Code Playgroud)

但是每当我单击编辑按钮时。什么都没有发生。我想知道缺少什么。

xxx*_*xxx 12

我花了几个小时在网上搜索解决我的 UIButton 在 UICollectionView 中不起作用的方法。让我发疯,直到我终于找到适合我的解决方案。而且我相信这也是正确的方法:破解命中测试。这是一个可以比修复 UICollectionView 按钮问题更深入(双关语)的解决方案,因为它可以帮助您将点击事件发送到隐藏在其他视图下的任何按钮,这些按钮阻止您的事件通过:

集合视图中单元格中的 UIButton 未在事件内部接收修饰

由于那个 SO 答案是在目标 C 中,我按照那里的线索找到了一个快速的解决方案:

http://khanlou.com/2018/09/hacking-hit-tests/

——

当我禁用单元格上的用户交互或我尝试过的任何其他各种答案时,没有任何效果。

我在上面发布的解决方案的美妙之处在于,您可以保留 addTarget 和选择器函数的习惯,因为它们很可能从来都不是问题。您只需要覆盖一个函数来帮助触摸事件到达目的地。

为什么解决方案有效:

在最初的几个小时里,我认为我的 addTarget 调用没有正确注册手势。事实证明,目标注册得很好。触摸事件根本没有到达我的按钮。

现实似乎来自我读过的任何数量的 SO 帖子和文章,UICollectionView Cells 旨在容纳一个动作,而不是多个动作。所以你应该只使用内置的选择操作。考虑到这一点,我认为解决此限制的正确方法不是破解 UICollectionView 以禁用滚动或用户交互的某些方面。UICollectionView 只是在做它的工作。在正确的方式是破解命中测试拦截它得到UICollectionView之前哪些项目,他们攻水龙头和人物。然后您只需向他们正在点击的按钮发送一个触摸事件,让您的普通东西完成工作。


我的最终解决方案(来自 khanlou.com 文章)是将我的 addTarget 声明和我的选择器函数放在我喜欢的任何地方(在单元格类或 cellForItemAt 覆盖中),并在覆盖 hitTest 函数的单元格类中。

在我的细胞课中,我有:

@objc func didTapMyButton(sender:UIButton!) {
    print("Tapped it!")
}
Run Code Online (Sandbox Code Playgroud)

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    guard isUserInteractionEnabled else { return nil }

    guard !isHidden else { return nil }

    guard alpha >= 0.01 else { return nil }

    guard self.point(inside: point, with: event) else { return nil }


    // add one of these blocks for each button in our collection view cell we want to actually work
    if self.myButton.point(inside: convert(point, to: myButton), with: event) {
        return self.myButton
    }

    return super.hitTest(point, with: event)
}
Run Code Online (Sandbox Code Playgroud)

在我的单元类 init 中,我有:

self.myButton.addTarget(self, action: #selector(didTapMyButton), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)


Sup*_*hon 7

在你的手机上试试这个

cell.contentView.isUserInteractionEnabled = false
Run Code Online (Sandbox Code Playgroud)


Gur*_*tth 0

您是否将类分配给单元格并将按钮与控制器链接,并将标识符分配给segue并使用断点来检查函数是否被调用

并像这样使用segue

func editGroupAction() {
    performSegue(withIdentifier: "editGroupSegue", sender: self)
    let src = self.sourceViewController as UIViewController
    let dst = self.destinationViewController as UIViewController
    src.navigationController.pushViewController(dst, animated:false)
}
Run Code Online (Sandbox Code Playgroud)