Jack tileman
Jack tileman

Reputation: 871

invalidateLayout with invalidateSupplementaryElements and UICollectionViewLayoutInvalidationContext

I am trying to refresh a Footer ( supplementaryElement). I am using the invalidatelayout method. Based on SO suggestions here. Apple documentation here.

I am getting the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'the invalidation context () sent to -[UICollectionViewFlowLayout invalidateLayoutWithContext:] is not an instance of type UICollectionViewFlowLayoutInvalidationContext or a subclass'

Implemented Code:

let footer_context = UICollectionViewLayoutInvalidationContext()
footer_context.invalidateSupplementaryElements(ofKind: "ActivitiesSelectMembersFooterView", at: [indexPath])
self.collectionView?.collectionViewLayout.invalidateLayout(with: footer_context)

Looks like invalidateLayout method is expecting UICollectionViewFlowLayoutInvalidationContext instead of UICollectionViewLayoutInvalidationContext.

I am using Xcode 8 and Swift 3.

My Storyboard in Xcode is here -

enter image description here

"Next" is the Footer that has the custom class "ActivitiesSelectMembersFooterView"

Upvotes: 10

Views: 5158

Answers (1)

jamesk
jamesk

Reputation: 3887

The error message accurately describes the problem. When calling invalidateLayout(with:) on a UICollectionViewFlowLayout object, you need to pass an instance of UICollectionViewFlowLayoutInvalidationContext.

Change this line:

let footer_context = UICollectionViewLayoutInvalidationContext()

To this:

let footer_context = UICollectionViewFlowLayoutInvalidationContext()

Upvotes: 9

Related Questions