2013-02-27 70 views

回答

0

您應該將自己的字典映射到標題視圖的索引路徑。在你的collectionView:viewForSupplementaryElementOfKind:atIndexPath:方法中,在返回之前將視圖放入字典中。在您的collectionView:didEndDisplayingSupplementaryView:forElementOfKind:atIndexPath:中,從字典中刪除視圖。

+7

將是很好,如果你歸於原作者:http://stackoverflow.com/a/13410537 – Anthony 2014-09-22 19:22:12

0
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    switch kind { 
    case UICollectionElementKindSectionHeader: 
     let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! HeaderCollectionReusableView 
     let gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didSelectSection(gesture:))) 
     headerView.addGestureRecognizer(gestureRecognizer) 
     return headerView 
    } 
} 

現在didSelectSection:

let indexPaths = self.collectionView?.indexPathsForVisibleSupplementaryElements(ofKind: UICollectionElementKindSectionHeader) 
for indexPath in indexPaths! { 
    if (gesture.view as! HeaderCollectionReusableView) == collectionView?.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: indexPath){ 
     print("found at : \(indexPath)") 
     break 
    } 
} 
+0

這是什麼代碼呢?它是如何解決問題的? – JJJ 2017-02-07 10:27:26

+0

只需將'viewForSupplementaryElementOfKind'中的tap按鈕添加到UICollectionReusableView,並在手勢選擇器中添加上面的代碼,就可以根據需要獲取頁眉或頁腳的indexPath。 我將更新代碼 – jovanpreet 2017-02-07 11:13:46

相關問題