简体   繁体   English

CollectionView 中的按钮不可点击

[英]Button inside CollectionView not clickable

I have a button in a custom cell of a collectionview.我在 collectionview 的自定义单元格中有一个按钮。 The collectionview is on a scrollview.集合视图位于滚动视图上。 For some reason, I am not able to click on the button.出于某种原因,我无法点击按钮。 I've checked that all my elements have User Interaction enabled.我已经检查过我的所有元素都启用了用户交互。

Here is my layout of the collection (I've hidden some sensitive data)这是我的收藏布局(我隐藏了一些敏感数据) 在此处输入图片说明

Here is my custom collection view cell:这是我的自定义集合视图单元格:

class MyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var connectButton: UIButton!

    var onConnectTap: (MyCollectionViewCell) -> Void)?
    @IBAction func connectButton(_ sender: Any) {
        onConnectTap?(self)
    }

    func populate(_ user: User) {
        nameLabel.text = user.name
     }
}

I have a xib file where a Touch Up Inside event of a button has been hooked up to the connectButton IBAction.我有一个 xib 文件,其中一个按钮的 Touch Up Inside 事件已连接到 connectButton IBAction。

And in my ViewController:在我的 ViewController 中:

MyCollectionView.register(UINib(nibName: "MyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell") 

Here's my collection view function in my ViewController:这是我的 ViewController 中的集合视图函数:

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

        let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell
        let user = users.values[indexPath.row]
        cell.populate(user)

        cell.onConnectTap = { (cell) in
            //do something
        }

        return cell

}

Nothing happens when I click on the button.当我点击按钮时什么也没有发生。 Am I missing something here?我在这里错过了什么吗? Is the scroll view interfering?滚动视图是否有干扰? Do I need to specifiy a addTarget?我需要指定一个 addTarget 吗? Or something else?或者别的什么?

After searching the entire web pretty much, I finally found the solution that was in the comment of this SO answer: https://stackoverflow.com/a/44908916/406322在几乎搜索了整个网络之后,我终于找到了这个 SO 答案的评论中的解决方案: https : //stackoverflow.com/a/44908916/406322

I needed to add this in MyCollectionViewCell:我需要在 MyCollectionViewCell 中添加这个:

self.contentView.isUserInteractionEnabled = false

I think the cell selection was hijacking the touch event.我认为细胞选择正在劫持触摸事件。

I'm facing the same issue and found the best solution after spending much time.我面临同样的问题,并在花了很多时间后找到了最佳解决方案。

cell.contentView.isUserInteractionEnabled = false

But its the perfect solution, and adds only one line in the cell for item method但它是完美的解决方案,并且只在单元格中为 item 方法添加了一行

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

    let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell

     cell.contentView.isUserInteractionEnabled = false
     return cell
}

Double-check the structure of the XIB file.仔细检查 XIB 文件的结构。 I lost time dealing with this issue (where the button in the XIB did not seem to respond), as the structure had a second embedded cell, rather than just one (AboutCell in my case).我在处理这个问题时浪费了时间(XIB 中的按钮似乎没有响应),因为该结构有第二个嵌入式单元,而不仅仅是一个(在我的情况下为 AboutCell)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CollectionView 中的 ImageView 和 Button - ImageView and Button inside CollectionView collectionView单元格中的按钮选择器 - selector for button inside collectionView cell 表格单元格内的按钮不可单击 - Button inside table cell not clickable 将动画添加到collectionview单元内的按钮 - Adding animation to a button inside a collectionview cell CollectionView 内的按钮重复第一个单元格信息 - Button inside the CollectionView repeating the first cell information CollectionView嵌套在Collectionview中 - CollectionView nested inside Collectionview 想要调整其中包含 CollectionView 和 Button 的 Horizontal StackView 的大小 - Want to resize Horizontal StackView that has CollectionView and a Button inside it 将目标添加到tableview单元格内的collectionview单元格的按钮 - Add target to button of collectionview cell inside tableview cell 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 单元格中的可单击的imageview和buttonview(UITableView或collectionView) - clickable imageview and buttonview in a cell(UITableView or collectionView)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM