2017-07-14 68 views
0

我使用UICollectionView,在滾動的UICollectionView特定部分我在寫UICollectionView的擴展,它工作正常在iPhone 6 &還對模擬器但在iPhone 5應用程序得到墜毀我調試,但無法找到問題所在。應用越來越墜毀,機上`layoutAttributesForSupplementaryElement`

這裏是我的代碼

extension UICollectionView { 

func scrollToIndexpathByShowingHeader(_ indexPath: IndexPath) { 
    let sections = self.numberOfSections 

    if indexPath.section <= sections { 

     let attributes = layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) 
     let topOfHeader = CGPoint(x: 0, y: attributes!.frame.origin.y - self.contentInset.top) 
     self.setContentOffset(topOfHeader, animated:false) 

    } 
} 

} 

let attributes = layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) 我的應用程序得到墜毀,墜毀的原因是:

reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: UICollectionElementKindSectionHeader at path <NSIndexPath: 0x1564e0b0> {length = 2, path = 18 - 1}'

請讓我知道什麼是問題,以及如何解決 在此先感謝:-)

回答

1

這是因爲單元格無法檢索layoutAttributes。如果由update創建的單元格不可見,layoutAttributesForItemAtIndexPath返回nil。

我想下面的解決方案將你。

覆蓋layoutAttributesForItemAtIndexPath必須使用此方法:

雨燕3.0 爲頭的方法,希望它會幫助你

override func layoutAttributesForItemscrollToSection(at_ indexPathsection: IndexPathInt) -> UICollectionViewLayoutAttributes? { 
    //set Contentoffset to particular section 
} 
+0

我不明白這一點。請多說一些。 @Jaydeep –

+0

上一個答案是正確的'layoutAttributesForItem(at:indexPath)'好心地重新編輯,以便我可以發現 –

+0

@asimabbasi,奇怪:) – Jaydeep