gpt4 book ai didi

ios - 在 Swift 中重写 UICollectionViewLayout 中的方法时出现错误

转载 作者:行者123 更新时间:2023-11-28 18:28:59 26 4
gpt4 key购买 nike

我需要对我们正在开发的应用程序进行更改,我不是全职 iOS 开发人员。我正在尝试为 iOS 应用程序获取类似 pinterest 的界面,并且正在阅读此处的教程:https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest

在他们的自定义 UICollectionViewLayout 中,他们覆盖了 layoutAttributesForElementsinRect 但我从 XCode 8 编译器中收到错误(尽管运行时使用旧版 Swift 语言版本设置为是)。

我得到的错误是:Method does not override any method from its superclass

缩写代码为:

class PinterestLayout: UICollectionViewLayout {

....
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {

var layoutAttributes = [UICollectionViewLayoutAttributes]()

// Loop through the cache and look for items in the rect
for attributes in cache {
if CGRectIntersectsRect(attributes.frame, rect ) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}

如果我将方法切换为私有(private),它会编译但不起作用,如果我删除覆盖,它会给我带来冲突。我确实想覆盖底层方法,但不确定如何让它工作。

最佳答案

您的方法签名有误。这实际上就是 override 关键字存在的原因。它确保 API 更改将像这样被捕获,而不是静默地不发生覆盖,从而导致难以诊断问题。

应该是

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?

关于ios - 在 Swift 中重写 UICollectionViewLayout 中的方法时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967470/

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