如何在swift中获取集合视图的标题?

时间:2016-12-16 08:28:07

标签: swift uicollectionview uicollectionviewdelegate

如何在DidSelectItemAtIndexPath上获取集合视图的标题,以便在选择项目时更改标题文本。

2 个答案:

答案 0 :(得分:4)

您可以使用:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    let indexHeaderForSection = NSIndexPath(row: 0, section: indexPath.section) // Get the indexPath of your header for your selected cell
    let header = collectionView.viewForSupplementaryElementOfKind(indexHeaderForSection)
}

答案 1 :(得分:3)

从iOS 9开始,您可以使用:

func supplementaryView(forElementKind elementKind: String, 
                 at indexPath: IndexPath) -> UICollectionReusableView? 

通过索引路径获取补充视图。

查看Apple's documentation