重新加载 UICollectionView 页眉或页脚? [英] Reload UICollectionView header or footer?

查看:29
本文介绍了重新加载 UICollectionView 页眉或页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据是在另一个更新 UICollectionView 标头的线程中获取的.但是,我还没有找到重新加载补充视图(例如页眉或页脚)的有效方法.

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 such as a header or footer.

我可以调用 collectionView reloadSections:,但这会重新加载整个部分,这是不必要的.collectionView reloadItemsAtIndexPaths: 似乎只针对单元格(不是补充视图).并且在标题本身上调用 setNeedsDisplay 似乎也不起作用.我错过了什么吗?

I can call collectionView reloadSections:, but this reloads the entire section which is unnecessary. collectionView reloadItemsAtIndexPaths: only seems to target cells (not supplementary views). And calling setNeedsDisplay on the header itself doesn't appear to work either. Am I missing something?

推荐答案

你也可以用(偷懒的方式)

You can also use (the lazy way)

collectionView.collectionViewLayout.invalidateLayout() // swift

[[_collectionView collectionViewLayout] invalidateLayout] // objc

更复杂的是提供上下文

collectionView.collectionViewLayout.invalidateLayout(with: context) // swift

[[_collectionView collectionViewLayout] invalidateLayoutWithContext:context] // objc

然后您可以自己创建或配置上下文以告知应更新的内容,请参阅:UICollectionViewLayoutInvalidationContext

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:)//获取特定的visibleSupplementaryViews(ofKind:)//所有可见的

visibleCells 的可见单元格也是如此.仅获取视图而不完全重新加载视图的优点是单元格保留了它的状态.当表格视图单元格使用滑动来删除/编辑/等时,这尤其适用于表格视图单元格,因为重新加载单元格后该状态会丢失.

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

这篇关于重新加载 UICollectionView 页眉或页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆