ios - UICollectionView 的 didSelectItemAt 仅在双击时调用

标签 ios swift xcode uicollectionview segue

我在使用 iOS11 模拟器的 UICollectionView 中遇到了一个奇怪的问题。在我的项目中,我有一个 UICollectionViewUIImageView 作为单元格,我通过将它拖动到 View Controller 来为单元格创建了 segue 作为 Triggered Segues。它工作得很好,但知道 segue 没有执行,所以我决定从单元格触发的 Segues 中删除 segue,我创建了一个从我的 View Controller 到目标 View Controller 的 segue,并从代码执行 segue

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCell
    if let url = URL(string: "\(StringResources.serverAddress)/Content/images/Files/Thumb/\(photos[indexPath.row])") {
        cell.image.downloadFrom(url: url)
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("clicked")
    performSegue(withIdentifier: "PhotoSegue", sender: collectionView.cellForItem(at: indexPath))
}

但它也不起作用,并且控制台中没有打印点击。我检查了单元格和 UIImageView 用户交互,UICollectionView 委托(delegate)也正常。我该如何解决?


编辑:我发现了问题。它只在我双击单元格时调用

最佳答案

有些事情你可以检查,一旦所有事情都按顺序进行,它应该可以工作:

  1. 检查您的 UICollectionView 是否同时设置了委托(delegate)和数据源。
  2. 检查您的 UIImageView userInteractionEnabled 属性是否设置为 false

根据文档:

Image views ignore user events by default. Normally, you use image views only to present visual content in your interface. If you want an image view to handle user interactions as well, change the value of its isUserInteractionEnabled property to true. After doing that, you can attach gesture recognizers or use any other event handling techniques to respond to touch events or other user-initiated events.

  1. 检查 UICollectionView 和父级是否都有。 userInteractionEnabled 属性设置为 true
  2. 检查是否没有其他 UIGestureRecognizers 捕获您的触摸事件。
  3. 检查没有网络请求卡住您的用户界面。

考虑到您提供的信息,我会尝试从单元格中删除 UIImageView 并确保在添加更多元素之前单元格触摸正常工作。

关于ios - UICollectionView 的 didSelectItemAt 仅在双击时调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46934714/

相关文章:

ios - SwiftUI Firestore 查询游标分页不起作用

ios - Xcode UICollectionView - 选择项目并发送到多个 UIViewControllers

ios - 使用 CommonCrypto 的 Swift AES 加密

ios - Swift:运行时:线程问题:MyApp.DownloadManager.progress.setter 中的数据竞争:Swift.Float at 0x7b1400209130

objective-c - Swift 中 if 语句/Obj-C 闭包中的非 Bool

iphone - Facebook 安装的参数不返回数据

ios - Xcode - 构建应用程序 - 问题

ios - 使用未声明的标识符 'FIRApp'

ios - 我们从服务器获取时间并更改为 iOS 7 中的本地时区?

ios - Invalid Code Signing Entitlements-in-app-payments 相关