本文介绍了UICollectionView:有没有办法只重新加载补充视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个部分都有一个带有 supplementaryView 的 uicollectionview.在每个 supplementaryView 我都有一些按钮.在 dataSource 方法 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 我使用 indexPath.section 设置按钮标签.当按下按钮时我做了一些委托方法并将部分(button.tag)作为参数发送.

I have an uicollectionview with supplementaryView for each section. In each supplementaryView I have some buttons.In dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: I'm setting the buttons tags with the indexPath.section. I made some delegate methods when a button is pressed and send the section(button.tag) as parameter.

一切正常,但问题来了:

Everything works fine, but here comes the problem:

  • 当我插入或删除一个部分时,它必须更新按钮标签,但我不想重新加载每个部分的所有项目.

我试过了:

  • 使用 dataSource 方法 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 获取补充视图,并为每个 supplementaryView 调用 layoutIfNeeded 方法,这样supplementaryViews 正在重新加载,但这种方法会增加我的 supplementaryViews.
  • collectionView.subviews 中的快速 for 并仅获取 supplementaryView 并调用 layoutIfNeeded 方法,但它不会重新加载,只是布局正在重新加载.
  • reloadSections: 方法,但这会重新加载部分中的所有项目
  • to get the supplementaryViews with the dataSource method collectionView:viewForSupplementaryElementOfKind:atIndexPath: and call layoutIfNeeded method for each supplementaryView, in this way the supplementaryViews are reloading, but this approach multiply my supplementaryViews.
  • a fast for in collectionView.subviews and get just the supplementaryView and call layoutIfNeeded method, but it doesn't reload, just the layout is reloading.
  • reloadSections: method, but this reload all items from the section

哦.. 我使用 NSFetchedResultsController(delegate) 来插入和删除部分.

Ohh.. And I use NSFetchedResultsController(delegate) for inserting and deleting sections.

谁能帮我解决这个问题?有没有办法只重新加载 supplementaryView?这种情况还有其他方法吗?

Can anyone help me with this? Is there a way to reload only the supplementaryView? Is there other approach for this scenario?

谢谢!

推荐答案

要重新加载补充视图,您可以使用 -invalidateLayoutWithContext:.

To reload supplementary views you can use -invalidateLayoutWithContext:.

请参阅 UICollectionViewLayoutInvalidationContext 和方法 -invalidateSupplementaryElementsOfKind:atIndexPaths: 的文档(自 iOS 8 起可用).

See documentation for UICollectionViewLayoutInvalidationContext and method -invalidateSupplementaryElementsOfKind:atIndexPaths: (available since iOS 8).

这篇关于UICollectionView:有没有办法只重新加载补充视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 21:48