我的 CollectionView header 中有图像,并且希望在单击按钮时访问它们。
我用过
let cell = collectionView(collectionView, viewForSupplementaryElementOfKind: UICollectionView.elementKindSectionHeader, at: headerIndexPath) as? ListItemHeaderReusableView
但这行代码重新加载了我不想要的 header 的覆盖函数,因为图像随后从 header 中删除了。
也尝试过
let ncell = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: indexPath) as? ListItemHeaderReusableView
由于某种原因,这会在模拟器上返回单元格,但在设备上返回 nil。
当标题不可见且内容未重新加载时,如何访问标题。
最佳答案
您可以将 header 保留为属性。
class ViewController: UIViewController {
let header = ListItemHeaderReusableView()
// here is your custom collectionView header
// you have it as a property
// so you retain it, it will not be reloaded when you scroll
}
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// do some custom config
return header
}
// more methods
}
关于ios - 滚动离开屏幕(即不可见)时如何访问 CollectionView header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58620886/