【发布时间】: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