我正在开发一个项目,其中在UITableView内部的UITableViewCell内部有一个UICollectionView . 我使用XIB作为我的UICollectionViewCell,它包含一个ImageView,另一个XIB用于我的UITableViewCell,它包含一个UICollectionView . 我已设法显示所需的东西,但是,我的自定义集合视图单元格没有响应任何触摸事件 . 经过一番研究,我尝试过:
在界面生成器中的UITableView和UITableViewCell中以编程方式关闭"User Interaction Enabled",同时在UICollectionView和UICollectionViewCell中打开"User Interaction Enabled"
在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的屏幕截图:
任何帮助表示赞赏 . 谢谢!