gpt4 book ai didi

swift - 应用程序在 `layoutAttributesForSupplementaryElement` 处崩溃

转载 作者:行者123 更新时间:2023-11-30 12:18:32 25 4
gpt4 key购买 nike

我正在使用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}'

请让我知道问题是什么以及如何解决预先感谢:-)

最佳答案

这是因为单元格无法检索layoutAttributes。如果更新创建的单元格不可见,则layoutAttributesForItemAtIndexPath返回nil。

我认为以下解决方案适合您。

重写layoutAttributesForItemAtIndexPath必须使用此方法:

swift 3.0对于 header 方法,希望对您有帮助

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

关于swift - 应用程序在 `layoutAttributesForSupplementaryElement` 处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45100623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com