La disposition de stream personnalisé UICollectionView se bloque lors du défilement

Je crée un flowLayout personnalisé avec des en-têtes de section.

voici mon flowLayout.swift

import UIKit class FlowLayout: UICollectionViewFlowLayout { override func layoutAtsortingbutesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAtsortingbutes]? { // let atsortingbutesForElementsInRect = super. var newAtsortingbutesForElementsInRect = [UICollectionViewLayoutAtsortingbutes]() // unwrap super's atsortingbutes guard let atsortingbutesForElementsInRect = super.layoutAtsortingbutesForElementsInRect(rect) else { return nil } // modify atsortingbutes var leftMargin: CGFloat = 0.0; for atsortingbutes in atsortingbutesForElementsInRect { let itemAtsortingbutesCopy = atsortingbutes.copy() as! UICollectionViewLayoutAtsortingbutes if( itemAtsortingbutesCopy.frame.size.width + leftMargin > self.collectionView?.frame.size.width) { leftMargin = 8.0 } if (itemAtsortingbutesCopy.frame.origin.x == self.sectionInset.left) { leftMargin = self.sectionInset.left } else { var newLeftAlignedFrame = itemAtsortingbutesCopy.frame newLeftAlignedFrame.origin.x = leftMargin itemAtsortingbutesCopy.frame = newLeftAlignedFrame } leftMargin += itemAtsortingbutesCopy.frame.size.width + 8 print(itemAtsortingbutesCopy.frame) newAtsortingbutesForElementsInRect.append(itemAtsortingbutesCopy) print(itemAtsortingbutesCopy) } return newAtsortingbutesForElementsInRect } } 

voici mon ViewController avec la fonction de délégué pour l'en-tête de section

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { if(sectionsArray.count == 0) { return 1 } else { return sectionsArray.count } } func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: Ssortingng, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { let headerView: TagHeader = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "TagHeader", forIndexPath: indexPath) as! TagHeader headerView.sectionLabel.text = sectionsArray[indexPath.section].header return headerView } 

Donc, quand je lance l'application, il me montre les en-têtes de section et tout fonctionne bien.

Mais quand je défile, l'application se bloque avec l'erreur suivante du débogueur

 2016-07-01 18:52:12.051 TagFlowLayout[15025:4704387] *** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UICollectionViewData.m:408 2016-07-01 18:52:12.054 TagFlowLayout[15025:4704387] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout atsortingbutes for supplementary item at index path (<NSIndexPath: 0x17e96f70> {length = 2, path = 0 - 0}) changed from <UICollectionViewLayoutAtsortingbutes: 0x17e7ab00> index path: (<NSIndexPath: 0x17d8fb30> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (8 0; 314 50); zIndex = 10; to <UICollectionViewLayoutAtsortingbutes: 0x17e9d770> index path: (<NSIndexPath: 0x17e96f70> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 314 50); zIndex = 10; without invalidating the layout' 

Vous devez invalider la layout existante avant la mise à jour, voir la fin du message d'erreur:

sans invalider la layout

Vous devez replace la méthode de la sous-class de UICollectionViewFlowLayout:

 override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return true } 

Pour plus de reference, consultez la documentation Apple pour UICollectionViewLayout