【发布时间】:2018-12-18 04:03:25
【问题描述】:
在我的 VC 中,我有一个从底部向上拉的视图。我在 viewDidLoad() 中设置并添加了一个 UICollectionView:
//Add and setup the collectionView
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowLayout)
collectionView?.register(PhotoCell.self, forCellWithReuseIdentifier: "photoCell")
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.backgroundColor = #colorLiteral(red: 0.9771530032, green: 0.7062081099, blue: 0.1748393774, alpha: 1)
collectionView?.allowsMultipleSelection = false
collectionView?.allowsSelection = true
pullUpView.addSubview(collectionView!)
pullUpView.bringSubview(toFront: collectionView!)
UICollectionView Delegate 方法位于扩展中,目前与 VC 位于同一代码文件中:
//MARK: - Extension CollectionView
extension MapVC: UICollectionViewDelegate, UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagesArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell {
let imageFromIndex = imagesArray[indexPath.item]
let imageView = UIImageView(image: imageFromIndex )
cell.addSubview(imageView)
return cell
} else {
return PhotoCell()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selected")
//Create PopVC instance, using storyboard id set in storyboard
guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC else { return }
popVC.passedImage = imagesArray[indexPath.row]
present(popVC, animated: true, completion: nil)
}
}
问题是当我点击一个单元格时,什么也没有发生。我在 didSelectItemAt 方法中放置了一个打印语句,但它永远不会被打印出来。所以,我的单元格永远不会被选中,或者至少 didSelectItemAt 方法永远不会被触发!
已经调试和尝试了几个小时,但我看不出有什么问题。任何帮助表示赞赏。也许有人可以从 Github 打开 mijn 项目,看看有什么问题,如果允许的话?
更新: 使用 Debug View Hierarchy,我看到一些令人不安的事情:每个 photoCell 都有多个(很多!)UIImageViews。我认为每个 photoCell 应该只有一个 UIImageView。我不知道是什么导致了这种行为?
【问题讨论】:
-
如果你长按,那么它会起作用吗?
-
github的链接在哪里
-
长按也不行,模拟器不行,物理设备不行
-
这里是 Github 链接github.com/Gakkienl/Pixel-City
-
由于缺少 podfile 和依赖项,您在 github 上的项目无法编译
标签: ios swift uicollectionview swift4