【问题标题】:UICollectionViewCell inside UITableViewCell not responding to actionsUITableViewCell 内的 UICollectionViewCell 不响应操作
【发布时间】:2018-01-25 11:44:21
【问题描述】:

我正在开发一个项目,其中 UITableView 内部的 UITableViewCell 内部有一个 UICollectionView。我为包含 ImageView 的 UICollectionViewCell 使用了 XIB,为包含 UICollectionView 的 UITableViewCell 使用了另一个 XIB。我已经设法显示所需的东西,但是,我的自定义集合视图单元格没有响应任何触摸事件。经过一番研究,我尝试了:

  • 在 Interface Builder 的 UITableView 和 UITableViewCell 中以编程方式关闭“用户交互已启用”,同时在 UICollectionView 和 UICollectionViewCell 中打开“用户交互已启用”
  • 在 UITableViewCell 中为 UICollectionView 设置委托,其中我的自定义 UITableViewCell 类实现了 UICollectionViewDelegate 和 UICollectionViewDataSource

     collectionView.delegate = self
     collectionView.dataSource = self
    
  • 使用 UICollectionView 的委托方法

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // my implementation here
    }
    
  • 为 cellForItemItemAt 中的每个单元格添加自定义 UITapGestureRecognizer

     let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyCustomTableViewCell.cellTapped))
     tapRecognizer.numberOfTapsRequired = 1
     cell.addGestureRecognizer(tapRecognizer) 
    
  • 使用 UITableViewCell、UICollectionView 和 UICollectionViewCell 的自定义实现,覆盖 hitTest 函数

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let hitView = super.hitTest(point, with: event) {
            if hitView is MyCustomCollectionViewCell {
                print("hitTest - MyCustomCollectionViewCell")
                return hitView
            } else {
                return nil
            }
        } else {
            return nil
        }
    }
    

这是我自定义的 TableViewCell XIB 的屏幕截图:

感谢任何帮助。谢谢!

【问题讨论】:

  • 尝试为集合视图提供固定高度。
  • 使用集合视图委托方法而不是点击手势。
  • @Ujesh 他们确实有一个固定的高度,但我不明白这有什么帮助
  • 这里是 'UITableView' 中的 'UICollectionView' 的教程:ashfurrow.com/blog/…
  • @Ujesh 我忘了在原始帖子中提到它,但我做到了。查看编辑版本

标签: ios swift uitableview uicollectionview


【解决方案1】:

事实证明,TableViewCells 现在有 contentViews,它阻止了我在里面的所有触摸。所以,我只是在我的 UITableViewDelegate 中使用了这个 -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ....
    cell.contentView.isUserInteractionEnabled = false
    ....
}

供参考:can't click UIButton inside a cell in UITableview

感谢:Stas Volskiy,非常乐于助人

【讨论】:

    【解决方案2】:

    可能你设置错了。 应该为 tableView、tableViewCell、collectionView、collectionViewCell 启用用户交互。如果你为 tableView 关闭它 - 那么它里面的所有东西都不会是交互式的。 在这里查看我的示例项目: https://github.com/1mperial8e/CollectionViewExample

    我重新创建了与您相同的情况 - tableViewCell 内的单元格中带有 imageView 的 collectionView。没有什么额外的,只是简单的观点。您可以比较设置并找出代码或情节提要设置中的问题所在。

    您不需要任何点击手势。 collectionView:didSelectItemAtIndexPath: 完美。

    【讨论】:

    • 您好,感谢您的回答。但是,正如我的帖子中所述,我正在使用 XIB 使用 UICollectionView 动态创建我的 UITableViewCell(其单元格也是从 XIB 创建的),与您的不同,单元格和集合视图以及集合视图单元格是在情节提要中创建的。我还按照建议启用了所有用户交互,但它仍然不起作用。
    • 我将使用 XIB 单元更新测试项目。您对 Objective-c 代码示例是否满意,还是我应该用 swift 编写?
    • @mugiwara528 已更新。现在单元格在 xib 文件中创建。从 git 签出新版本
    • 您好!我详细关注了您的项目,但它仍然对我不起作用!我注意到,当我单击我的单元格时,它会以灰色突出显示,但您的单元格不会。这意味着什么?
    • hmm .. 我猜您的 TableViewCell 已被选中,这就是为什么没有将事件触发到 CollectionView 的原因。尝试选择您的 TableView。在我的示例中,collectionView 涵盖了 tableView 单元格的整个大小,这就是它不选择的原因。如果有帮助,请告诉我,然后我会更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多