应用程序在`layoutAttributesForSupplementaryElement`上崩溃

时间:2017-07-14 10:23:41

标签: swift swift3 uicollectionview uicollectionviewlayout

我正在使用UICollectionView,在滚动到UICollectionView的特定部分时,我在UICollectionView上编写了一个在iPhone 6及其上工作正常的扩展程序。也在模拟器上,但在iPhone 5应用程序崩溃我调试它但无法找到问题所在。

这是我的代码

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 个答案:

答案 0 :(得分:0)

这是因为单元格无法检索layoutAttributes。如果不会显示将由update创建的单元格,则layoutAttributesForItemAtIndexPath将返回nil。

我认为下面的解决方案是你。

覆盖layoutAttributesForItemAtIndexPath必须使用此方法:

Swift 3.0 对于标题方法,希望它会帮助你

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