Ios UICollectionView自定义流布局在滚动时崩溃
我正在创建带有节标题的自定义flowLayout 这是我的flowLayout.swiftIos UICollectionView自定义流布局在滚动时崩溃,ios,iphone,uicollectionview,flowlayout,Ios,Iphone,Uicollectionview,Flowlayout,我正在创建带有节标题的自定义flowLayout 这是我的flowLayout.swift import UIKit class FlowLayout: UICollectionViewFlowLayout { override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { // let attributesForEleme
import UIKit
class FlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
// let attributesForElementsInRect = super.
var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
// unwrap super's attributes
guard let attributesForElementsInRect = super.layoutAttributesForElementsInRect(rect) else { return nil }
// modify attributes
var leftMargin: CGFloat = 0.0;
for attributes in attributesForElementsInRect {
let itemAttributesCopy = attributes.copy() as! UICollectionViewLayoutAttributes
if( itemAttributesCopy.frame.size.width + leftMargin > self.collectionView?.frame.size.width)
{
leftMargin = 8.0
}
if (itemAttributesCopy.frame.origin.x == self.sectionInset.left) {
leftMargin = self.sectionInset.left
} else {
var newLeftAlignedFrame = itemAttributesCopy.frame
newLeftAlignedFrame.origin.x = leftMargin
itemAttributesCopy.frame = newLeftAlignedFrame
}
leftMargin += itemAttributesCopy.frame.size.width + 8
print(itemAttributesCopy.frame)
newAttributesForElementsInRect.append(itemAttributesCopy)
print(itemAttributesCopy)
}
return newAttributesForElementsInRect
}
}
这是我的ViewController,具有用于节标题的委派功能
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
if(sectionsArray.count == 0)
{
return 1
}
else
{
return sectionsArray.count
}
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, 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
}
因此,当我运行应用程序时,它会显示分区标题,一切正常
但当我滚动时,应用程序崩溃,调试器出现以下错误
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 attributes for supplementary item at index path (<NSIndexPath: 0x17e96f70> {length = 2, path = 0 - 0}) changed from <UICollectionViewLayoutAttributes: 0x17e7ab00> index path: (<NSIndexPath: 0x17d8fb30> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (8 0; 314 50); zIndex = 10; to <UICollectionViewLayoutAttributes: 0x17e9d770> index path: (<NSIndexPath: 0x17e96f70> {length = 2, path = 0 - 0}); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 314 50); zIndex = 10; without invalidating the layout'
2016-07-01 18:52:12.051 TagFlowLayout[15025:4704387]***在-[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]***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“索引路径({length=2,path=0-0})处补充项的布局属性已从索引路径更改为:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(80;31450);zIndex=10;索引路径:({length=2,path=0-0});元素种类:(UICollectionElementKindSectionHeader);帧=(0;314 50);zIndex=10;而不会使布局的
您需要在更新之前使现有布局无效,请参阅错误消息的结尾:
不会使布局无效
您应该重写UICollectionViewFlowLayout子类中的方法:
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
有关更多参考信息,请参阅