2016-10-10 23 views
0

我需要對正在開發的應用程序進行更改,而不是全職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 
    } 

如果我的方法切換到私人它編譯但不工作,如果我刪除重寫,它給我是一個衝突。我確實想重寫底層方法,但不知道如何讓這個工作。

回答

5

您有錯誤的方法簽名。這實際上是override關鍵字存在的原因。它可以確保API更改會像這樣被捕獲,而不是無聲無息地覆蓋,從而導致難以診斷問題。

應該

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

這是因爲所提到的教程代碼通過SWIFT 2.X寫的,不是3.0 –

+0

@Ahmad是的,這是根本原因。 (現在)濫用「override」關鍵字所引發的錯誤是可以讓你抓住它的症狀。 – Alexander

+0

thx - 這是問題 – timpone

相關問題