CollectionView选择不起作用

时间:2016-12-27 13:05:55

标签: swift uicollectionview

我试图在我的UICollectionView中选择一个单元但它不起作用。我看了很多教程但没有人帮忙,我做的和他们完全一样,但是我的工作没有。我希望有人可以帮助我!

CollectionView创建代码:

colDef.name

选择代码:

func createCollectionView() {
    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: view.frame.size.width / 2.75, height: view.frame.size.width / 2.75)
    layout.minimumLineSpacing = 10
    layout.scrollDirection = .vertical

    photoCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width / 1.2, height: view.frame.size.height - biographyLabel.center.y - 75 -
        navigationController!.navigationBar.frame.size.height), collectionViewLayout: layout)
    photoCollectionView.backgroundColor = UIColor.white
    photoCollectionView.showsVerticalScrollIndicator = false
    photoCollectionView.showsHorizontalScrollIndicator = false
    photoCollectionView.allowsSelection = true
    photoCollectionView.delegate = self
    photoCollectionView.dataSource = self
    photoCollectionView.register(PhotosCollectionViewCell.self, forCellWithReuseIdentifier: "photoCell")
    photoCollectionView.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height - (photoCollectionView.frame.size.height / 2) - navigationController!.navigationBar.frame.size.height)

    view.addSubview(photoCollectionView)
}

感谢您的帮助!

更新:

创建单元代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("test")
}

1 个答案:

答案 0 :(得分:0)

我通过在createCollectionView函数中添加view.gestureRecognizers?.removeAll()来修复它。