Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CollectionView内的按钮不可单击_Ios_Swift_Uicollectionview_Uicollectionviewcell_Swift4 - Fatal编程技术网

Ios CollectionView内的按钮不可单击

Ios CollectionView内的按钮不可单击,ios,swift,uicollectionview,uicollectionviewcell,swift4,Ios,Swift,Uicollectionview,Uicollectionviewcell,Swift4,我在collectionview的自定义单元格中有一个按钮。collectionview位于scrollview上。由于某种原因,我无法点击按钮。我已经检查了我的所有元素是否都启用了用户交互 这是我的收藏布局(我隐藏了一些敏感数据) 这是我的自定义集合视图单元格: class MyCollectionViewCell: UICollectionViewCell { @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak va

我在collectionview的自定义单元格中有一个按钮。collectionview位于scrollview上。由于某种原因,我无法点击按钮。我已经检查了我的所有元素是否都启用了用户交互

这是我的收藏布局(我隐藏了一些敏感数据)

这是我的自定义集合视图单元格:

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
     }
}
我有一个xib文件,其中一个按钮的内部修补事件已连接到connectButton iAction

在我的ViewController中:

MyCollectionView.register(UINib(nibName: "MyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "cell") 
以下是我的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

}

当我点击按钮时,什么也没有发生。我是不是遗漏了什么?滚动视图有干扰吗?我需要指定一个addTarget吗?还是别的什么?

在搜索了整个网站之后,我终于找到了这个答案的注释中的解决方案:

我需要在MyCollectionViewCell中添加以下内容:

self.contentView.isUserInteractionEnabled = false

我认为单元格选择劫持了触摸事件。

仔细检查XIB文件的结构。我浪费了时间处理这个问题(XIB中的按钮似乎没有响应),因为该结构有第二个嵌入单元,而不是一个(在我的例子中是AboutCell)


我也面临同样的问题,在花了很多时间后找到了最好的解决方案

cell.contentView.isUserInteractionEnabled = false
但这是一个完美的解决方案,在单元格中只为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
}

connectButton
是否也未调用?您是否在interface builder中正确连接了它?它没有被调用,是的,我已经在IB中仔细检查了它是否正确连接。创建按钮的委托方法您应该尝试将集合视图及其父滚动视图clipsToBounds属性设置为
true
。由于您的集合视图位于滚动视图中,滚动视图的大小可能会很有挑战性,而且您可能会看到单元格,但父视图的大小不正确,从而打乱了响应链。@DirtyHenry和alreayd在IB中都将“剪辑到边界”设置为true。这对我来说很好,在尝试全天cell.contentView.isUserInteractionEnabled=false和public func textView(textView:UITextView,应该与URL:URL交互,在characterRange:NSRange中)->Bool{print(URL)return true}哇,这太晦涩了。伟大的发现!如果此解决方案对您有效,可能是因为在MyCollectionViewCell中,您没有正确地将按钮添加为
contentView
的子视图(或子视图的子视图);您可能正在将按钮添加到
视图中。因此,解决方案-您不应该禁用
contentView
上的交互,而应该将按钮添加到
contentView
而不是
view
。我尝试了这个方法,但不起作用:(请检查要单击的视图或按钮,确保用户交互已启用或未启用@Chandni