Reload UICollectionView header or footer?

前端 未结 10 1196
余生分开走
余生分开走 2020-12-08 02:35

I have some data that is fetched in another thread that updates a UICollectionView\'s header. However, I\'ve not found an efficient way of reloading a supplementary view suc

10条回答
  •  情歌与酒
    2020-12-08 03:10

    You can also use (the lazy way)

    collectionView.collectionViewLayout.invalidateLayout() // swift
    
    [[_collectionView collectionViewLayout] invalidateLayout] // objc
    

    More complex would be to provide a context

    collectionView.collectionViewLayout.invalidateLayout(with: context) // swift
    
    [[_collectionView collectionViewLayout] invalidateLayoutWithContext:context] // objc
    

    You can then make a or configure the context yourself to inform about what should be updated see: UICollectionViewLayoutInvalidationContext

    It has a function in there that you can override:

    invalidateSupplementaryElements(ofKind:at:) // swift
    

    Another option is (if you have already loaded the correct header/footer/supplementary view) and you only want to update the view with the new data than you can use one of the following functions to retrieve it:

    supplementaryView(forElementKind:at:) // get specific one visibleSupplementaryViews(ofKind:) // all visible ones

    Same goes for visible cells with visibleCells. The advantage of just getting the view and not reloading a view entirely is that the cells retains it state. This is espically nice with table view cells when they use swipe to delete/edit/etc since that state is lost after reloading the cell.

    If you feel fanatic you can of course also write some extensions to retrieve only cells/supplementary views of a given kind using generics

    if let view = supplementaryView(forType: MySupplementaryView.self, at: indexPath) {
        configure(view, at indexPath)
    }
    

    this assumes that you have a function that registers/dequeues views in example with their class name. I made a post about this here

提交回复
热议问题