2014-06-11 23 views
3

UIScrollView編譯爲UITableViewCell時出現錯誤。編譯器不喜歡用Swift編寫的UIScrollViewDelegate

class MyItemTableViewCell: UITableViewCell, UIScrollViewDelegate { 

... 

var scrollView: UIScrollView = { 
     let scroll = UIScrollView() 
     scroll.showsHorizontalScrollIndicator = false 
     scroll.delegate = self 
     return scroll; 
     }() 

... 

func scrollViewDidScroll(scrollView: UIScrollView!) { 
    ... 
    } 

func scrollViewWillEndDragging(scrollView: UIScrollView!, velocity: CGPoint, targetContentOffset: CMutablePointer<CGPoint>) { 
    ... 
    } 
... 
} 

編譯器給我一個錯誤上說,Type MyItemTableViewCell does not conform to protocol ‘UIScrollViewDelegate’

沒關係的事實,即在協議的方法都記錄爲可選(https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html)行scroll.delegate = self,但我有兩個實現。

編譯器對我有什麼要求?

感謝

+0

更改'targetContentOffset'鍵入inout CGPoint? –

+0

@JackWu:試過了 - 沒有運氣。 :-( – Joseph

+0

@David:我根據文檔對其進行了建模:https://developer.apple.com/library/prerelease/iOS/documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewWillEndDragging:withVelocity:targetContentOffset: – Joseph

回答

3

,在初始週期,直到所有屬性都被初始化自我是不可用的。將其更改爲:

class MyItemTableViewCell: UITableViewCell, UIScrollViewDelegate { 

    var scrollView: UIScrollView 

    func scrollViewDidScroll(scrollView: UIScrollView!) { 
    } 

    func scrollViewWillEndDragging(scrollView: UIScrollView!, velocity: CGPoint, inout targetContentOffset: CGPoint) { 
    } 

    init() { 
     scrollView = UIScrollView() 
     scrollView.showsHorizontalScrollIndicator = false 
     super.init(style:UITableViewCellStyle.Default, reuseIdentifier:"cell") 
     scrollView.delegate = self 
    } 
} 
+0

'inout X:T'是'X:CMutablePointer '的同義詞,因此請使用哪一個更適合你。 –

+0

當然!它還沒有被引用 - 我明白了! – Joseph

+0

我可以在我的ViewController中調用func scrollViewDidScroll嗎? –