ios - 滚动离开屏幕(即不可见)时如何访问 CollectionView header

标签 ios swift uicollectionview

我的 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/

相关文章:

ios - Xcode Storyboard - 约束大于或等于不起作用

ios - SIGABRT 每次触摸点击,不一致的 iPad iOS

ios - 它怎么不知道AppDelegate是什么?

ios - 具有动态约束的 IBDesignable View : Misplaced Children when Rendering

ios - 如何防止 uicollectionview 的单元格重用

ios - 如何根据 segmentedControl 索引更改数据

iphone - View Controller 和方向 iOS 中的 View Controller

ios - 在 Xcode UI 测试中等待 30 秒

arrays - 如何根据现有数组 Swift 3.0 检查按下按钮的顺序

swift - 如何访问我的存储并将其放置在我的收藏 View 中