繁体   English   中英

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

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

这是我的问题。 我做了一个继承自UIViewController,UICollectionViewDataSource和UICollectionViewDelegate的View Controller。 所以,我有这个:

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

然后,我制作了一个自定义视图,它具有一个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!)
    }
}

因此,在我的视图控制器中,我在viewDidLoad方法中使用以下代码设置了委托方法和功能:

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

这样,永远不会调用方法didSelectItemAtIndexPath。 是的,我有一个自定义单元格收集视图(在xib中),但我什至没有图形元素(为空),并且未调用该方法。 我已经实现了几种在Internet上找到的解决方案,但是没有任何效果。 有人知道会发生什么吗?

您是否尝试过从Storyboard或Xib将UICollectionViewCell连接到文件的所有者委托和dataSource? 也许这可以帮助

您只需将自定义视图的userInteractionEnabled设置为true 如果父视图未启用用户交互,则其子视图将无法识别任何触摸事件。

暂无
暂无

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

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