UICollectionView: Is there a way to reload only th

2019-02-11 22:11发布

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:

  • when I insert or delete a section it must update the buttons tags, but I don't want to reload all the items from each section.

I've tried:

  • 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

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

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

Thank You!

2条回答
迷人小祖宗
2楼-- · 2019-02-11 22:29

Instead of reloading these views, getting a reference to each visible supplementary view so that you can change the tag might be sufficient for your needs.

There is a simple method you can use to do this:

let supplementaryViews = collectionView.visibleSupplementaryViews(ofKind: "identifier")

https://developer.apple.com/documentation/uikit/uicollectionview/1618026-visiblesupplementaryviews

查看更多
聊天终结者
3楼-- · 2019-02-11 22:44

To reload supplementary views you can use -invalidateLayoutWithContext:.

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

查看更多
登录 后发表回答