ios - UICollectionView 自定义流布局在滚动时崩溃
<p><p>我正在创建带有节标题的自定义 flowLayout。</p>
<p>这是我的 flowLayout.swift</p>
<pre><code>import UIKit
class FlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> ? {
// let attributesForElementsInRect = super.
var newAttributesForElementsInRect = ()
// 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
}
}
</code></pre>
<p>这是我的 ViewController,带有节标题的委托(delegate)功能</p>
<pre><code>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.header
return headerView
}
</code></pre>
<p>所以当我运行应用程序时,它会显示部分标题,一切正常。</p>
<p>但是当我滚动时,应用程序崩溃并出现以下来自调试器的错误</p>
<pre><code>2016-07-01 18:52:12.051 TagFlowLayout *** Assertion failure in -, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UICollectionViewData.m:408
2016-07-01 18:52:12.054 TagFlowLayout *** 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'
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>更新前需要使现有布局失效,见报错文末:</p>
<blockquote>
<p>without invalidating the layout</p>
</blockquote>
<p>你应该重写 UICollectionViewFlowLayout 子类的方法:</p>
<pre><code>override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
</code></pre>
<p>更多引用见<a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewLayout_class/index.html#//apple_ref/occ/instm/UICollectionViewLayout/invalidateLayout" rel="noreferrer noopener nofollow">Apple Documentation for UICollectionViewLayout</a> </p></p>
<p style="font-size: 20px;">关于ios - UICollectionView 自定义流布局在滚动时崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38146913/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38146913/
</a>
</p>
页:
[1]