2016-12-16 24 views
0

我有一个UICollectionView,有几个部分和标题,并且我想检测节标题上的点击。Swift 3用于在collectionView标题上进行点击检测的工具

它的工作罚款感谢细胞

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 

但并没有什么特定的节头。

我试图在collectionView上实现一个tapGestureRecognizer,它正在工作,但是在那种情况下,上面的函数不再被触发。

有没有简单的方法来实现单元格以及部分标题上的点击检测?

感谢您的帮助:)

+1

你可以在标题视图本身实现一个'UITapGestureRecognizer'。点击标题视图应该触发与选择相应单元格相同的操作,对吧? – Randy

回答

0

可以设置空白按钮头查看然后设置target它。 如果你想执行传送action每个headerView点击那么你必须设置一些tag,因为它的indexPath.section

2

解决方案是将tapGestureRecognizer直接附加在部分上,而不是在collectionView上。并感谢John为标签提示。

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    ... 
    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "inputStartHeader", for: indexPath) as! GameInputStartHeader 
    headerView.tag = indexPath.section 

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(tapDetected)) 
    headerView.addGestureRecognizer(tapGestureRecognizer) 
    ... 
} 
+0

这里小心点,每次出列标题时最终都会有很多识别器。要么检查识别器是否已经存在,要么在添加识别器之前删除识别器。 – palme