我需要對正在開發的應用程序進行更改,而不是全職iOS開發人員。我試圖讓像界面的iOS應用Pinterest的和我通過教程在這裏工作:https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest在Swift中重寫UICollectionViewLayout中的方法,出現錯誤
在其定製的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
}
如果我的方法切換到私人它編譯但不工作,如果我刪除重寫,它給我是一個衝突。我確實想重寫底層方法,但不知道如何讓這個工作。
這是因爲所提到的教程代碼通過SWIFT 2.X寫的,不是3.0 –
@Ahmad是的,這是根本原因。 (現在)濫用「override」關鍵字所引發的錯誤是可以讓你抓住它的症狀。 – Alexander
thx - 這是問題 – timpone