简体   繁体   English

(快速)在外部视图中定义的UICollectionView中的didSelectItemAtIndexPath不起作用

[英](Swift) didSelectItemAtIndexPath in UICollectionView defined in external view doesn't work

This is my problem. 这是我的问题。 I have made a View Controller which inherited from UIViewController, UICollectionViewDataSource and UICollectionViewDelegate. 我做了一个继承自UIViewController,UICollectionViewDataSource和UICollectionViewDelegate的View Controller。 So, I have this: 所以,我有这个:

class myController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
...
}

And then, I made a custom view that it has a UICollectionView, something like this: 然后,我制作了一个自定义视图,它具有一个UICollectionView,如下所示:

class externalCollectionView: UIView {
    var myCollection: UICollectionView?

    override init(frame: CGRect){
        super.init(frame: frame)
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        layout.itemSize = CGSize(width: 130, height: 130)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10

        myCollection = UICollectionView(frame: CGRectMake(0, 120, frame.size.width, frame.size.height - 120), collectionViewLayout: layout)
        myCollection!.registerNib(UINib(nibName: "customCellView", bundle: nil), forCellWithReuseIdentifier: "myItem")

        self.addSubview(myCollection!)
    }
}

So, into my view controller I set delegate methods and features with this code in the viewDidLoad method: 因此,在我的视图控制器中,我在viewDidLoad方法中使用以下代码设置了委托方法和功能:

    customView = externalCollectionView(frame: self.view.frame)
    customView.myCollection!.dataSource = self
    customView.myCollection!.delegate = self
    customView.myCollection!.userInteractionEnabled = true
    customView.myCollection!.allowsMultipleSelection = true

with this, the method didSelectItemAtIndexPath never is called. 这样,永远不会调用方法didSelectItemAtIndexPath。 Yes, I have a custom cell collection view (in a xib), but I even have made it without graphical elements (empty) and the method is not called. 是的,我有一个自定义单元格收集视图(在xib中),但我什至没有图形元素(为空),并且未调用该方法。 I have implemented several solutions that I have found on Internet but nothing works. 我已经实现了几种在Internet上找到的解决方案,但是没有任何效果。 Anyone have idea what is happen? 有人知道会发生什么吗?

have you tried to connect from Storyboard or Xib your UICollectionViewCell to file's owner delegate and dataSource? 您是否尝试过从Storyboard或Xib将UICollectionViewCell连接到文件的所有者委托和dataSource? maybe this could help 也许这可以帮助

You just have to set userInteractionEnabled to true for the custom view. 您只需将自定义视图的userInteractionEnabled设置为true If the parent view has no user interaction enabled, then it's subviews won't recognise any touch events. 如果父视图未启用用户交互,则其子视图将无法识别任何触摸事件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM